当点击返回按钮在活动组,它不工作



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

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

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

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");
               ......
    }
}

提前感谢你们!!

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

@Override
    public void onBackPressed() {
       return;
    } 

相关内容

  • 没有找到相关文章

最新更新