当点击活动组中的后退按钮时,它不起作用

发布于 2024-12-09 21:50:55 字数 3130 浏览 1 评论 0原文

在我的应用程序中,我使用带有五个选项卡的TabActivity,每个选项卡包含一个ActvityGroup,每个ActivityGroup管理多个Activity。其中一个ActivtyGroup有两个Activies:A和B,当单击A中的按钮时,它会跳转到B。返回A,我想在B中显示一些更改的数据。我遇到的是:

首先,如果我启动B并且B中的某些小部件获得焦点,当单击后退按钮时,应用程序直接退出。我调试它并找到完成B时它完成了应用程序同时。第二,如果我启动 B 并且没有任何小部件获得焦点,当单击后退按钮时,B 中的 onKeyDown() 不起作用。

这是我的活动组,我使用 viewanimator 作为容器:

public class Group extends ActivityGroup 
{

    private Stack<String>aString;               
private LocalActivityManager manager;
private ViewAnimator animator;
private int ID;                              

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.group_products);

    aString = new Stack<String>();
    animator = (ViewAnimator) findViewById(R.id.pro_va_animator);
    manager = getLocalActivityManager();

    Intent intent = new Intent(this,Products_Category.class);
    startActivity(intent);
}

@Override
public void startActivity(Intent intent) 
{
    String id = "id" + ID++;
    aString.push(id);

    View view = manager.startActivity(id, intent).getDecorView();
    animator.addView(view);
    animator.setDisplayedChild(aString.size() - 1);
}

@Override
public void startActivityForResult(Intent intent,int requestCode)
{
    String id = "id" + ID++;
    aString.push(id);
    View view = manager.startActivity(id, intent).getDecorView();
    animator.addView(view);
    animator.setDisplayedChild(aString.size() - 1);
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) 
{
    if (keyCode == KeyEvent.KEYCODE_BACK) 
    {
        int size = aString.size();
        if (size > 0) 
        {
            String topId = aString.pop();
            View view = manager.destroyActivity(topId, true).getDecorView();
            animator.removeView(view);
            if (size > 1) 
            {
                return true;
            }
        }
    }
    return super.onKeyDown(keyCode, event);
}

@Override
protected void onActivityResult(int requestCode,int resultCode,Intent data)
{
    super.onActivityResult(requestCode, resultCode, data);

    A subActivity=(A)(manager.getCurrentActivity());
    A.handleActivityResult(requestCode, resultCode, data);
    break;

}
}

在 B 类中,我重写 onKeyDown() 以在捆绑包中添加一些更改的数据:

@Override
public boolean onKeyDown(int keyCode,KeyEvent event)
{
    if(keyCode==KeyEvent.KEYCODE_BACK)
    {
        Log.e("prodcuts_3","inner");
        Bundle bundle=new Bundle();
        bundle.putString("name",name);
        bundle.putString("price",price);
        bundle.putString("quantity",quantity);

        this.setResult(RESULT_CANCELED,this.getIntent().putExtras(bundle));
        this.finish();
        return this.getParent().onKeyDown(keyCode, event);
    }
    return super.onKeyDown(keyCode, event);
}

在 A 类中:

public void handleActivityResult(int requestCode,int resultCode,Intent data)
{
    if(resultCode==RESULT_CANCELED)
    {
        String price=data.getExtras().getString("price");
               ......
    }
}

提前谢谢你们!

In my app,i use a TabActivity with five tabs,each tab contain a ActvityGroup,each ActivityGroup manage more than one Activity.one of ActivtyGroups have two Activies:A and B, when click a button in A,it jump to B.when return to A,i want to display some changed data in B.what i meet is:

first, if i start B and some widget in B get focus,when click back button,app exit directly.i debug it and find when finish B it finish the app at the same time.second,if i start B and none of any widget get focus,when click back button,onKeyDown() in B does not work.

Here is my activitygroup,i ues viewanimator as container:

public class Group extends ActivityGroup 
{

    private Stack<String>aString;               
private LocalActivityManager manager;
private ViewAnimator animator;
private int ID;                              

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.group_products);

    aString = new Stack<String>();
    animator = (ViewAnimator) findViewById(R.id.pro_va_animator);
    manager = getLocalActivityManager();

    Intent intent = new Intent(this,Products_Category.class);
    startActivity(intent);
}

@Override
public void startActivity(Intent intent) 
{
    String id = "id" + ID++;
    aString.push(id);

    View view = manager.startActivity(id, intent).getDecorView();
    animator.addView(view);
    animator.setDisplayedChild(aString.size() - 1);
}

@Override
public void startActivityForResult(Intent intent,int requestCode)
{
    String id = "id" + ID++;
    aString.push(id);
    View view = manager.startActivity(id, intent).getDecorView();
    animator.addView(view);
    animator.setDisplayedChild(aString.size() - 1);
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) 
{
    if (keyCode == KeyEvent.KEYCODE_BACK) 
    {
        int size = aString.size();
        if (size > 0) 
        {
            String topId = aString.pop();
            View view = manager.destroyActivity(topId, true).getDecorView();
            animator.removeView(view);
            if (size > 1) 
            {
                return true;
            }
        }
    }
    return super.onKeyDown(keyCode, event);
}

@Override
protected void onActivityResult(int requestCode,int resultCode,Intent data)
{
    super.onActivityResult(requestCode, resultCode, data);

    A subActivity=(A)(manager.getCurrentActivity());
    A.handleActivityResult(requestCode, resultCode, data);
    break;

}
}

In class B,i rewrite onKeyDown() to add some changed data in bundle:

@Override
public boolean onKeyDown(int keyCode,KeyEvent event)
{
    if(keyCode==KeyEvent.KEYCODE_BACK)
    {
        Log.e("prodcuts_3","inner");
        Bundle bundle=new Bundle();
        bundle.putString("name",name);
        bundle.putString("price",price);
        bundle.putString("quantity",quantity);

        this.setResult(RESULT_CANCELED,this.getIntent().putExtras(bundle));
        this.finish();
        return this.getParent().onKeyDown(keyCode, event);
    }
    return super.onKeyDown(keyCode, event);
}

in class A:

public void handleActivityResult(int requestCode,int resultCode,Intent data)
{
    if(resultCode==RESULT_CANCELED)
    {
        String price=data.getExtras().getString("price");
               ......
    }
}

thank you guys in advances!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

铁轨上的流浪者 2024-12-16 21:50:55

将以下内容添加到您的类中,该类是从扩展 TabGroupActivity 的类中调用的

@Override
    public void onBackPressed() {
       return;
    } 

add the following to your class which is called from class which extends TabGroupActivity

@Override
    public void onBackPressed() {
       return;
    } 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文