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

时间:2011-10-12 17:55:06

标签: android android-activity android-tabhost activitygroup

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

首先,如果我启动B并且B中的某个小部件获得焦点,当单击后退按钮时,app直接退出。我调试它并在完成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()以在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);
}

在A班:

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

谢谢你们的进步!!

1 个答案:

答案 0 :(得分:1)

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

的类调用
@Override
    public void onBackPressed() {
       return;
    }