如何在ActivityGroup中使用startActivityForResult()?

时间:2011-10-29 06:18:45

标签: android android-tabhost activitygroup android-activity

我使用TabActivity和一些标签,每个标签包含一个ActvityGroup,每个ActivityGroup管理多个Activity.one的ActivtyGroups有三个Activies:A,B和C.

首先创建A,当用户单击A中的按钮时,它会跳转到B.

在B中有一些重要的数据可以在C中编辑,当点击B中的“编辑”按钮时,它会跳转到C.

如果在C中编辑了一些数据,当我点击后退按钮时,我想在B中修改相同的数据。 让我疯狂的是当我在C级使用“finish()”时,我的应用程序直接退出。

我在网上搜索了很多解决方案,但是没有一个适合我的情况,我不知道哪里出错了,请帮我或者给我一个如何在ActivityGroup中使用startActivityForResult()的例子

这是我的小组:

public class MyGroup extends ActivityGroup 
{

private int ID=0;
private AlertDialog dialog;
private Stack<View>history;                
private LocalActivityManager manager;

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);

    history = new Stack<View>();
    manager = getLocalActivityManager();

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

@Override
public void startActivity(Intent intent) 
{
    View view = manager.startActivity(""+ID++,intent).getDecorView();
    history.push(view);
    setContentView(view);
}

@Override
public void startActivityForResult(Intent intent,int requestCode)
{
    // super.startActivityForResult(intent, requestCode);
    View view = manager.startActivity(""+ID++,intent).getDecorView();
    history.push(view);
    setContentView(view);
}

/*
 * if user edited data in C.class.
 * when C.class finished,refresh data in the B.class
 */
@Override
protected void onActivityResult(int requestCode,int resultCode,Intent data)
{
    Log.e("MyGroup","running");
    super.onActivityResult(requestCode, resultCode, data);
    if(resultCode==RESULT_OK)
    {
           //modify data in B.java
        B subActivity=(B)(manager.getCurrentActivity());
        subActivity.handleActivityResult(requestCode, resultCode, data);
    }
}

/*
 * when press back button, manage which page to show next
 * if there is one page in stack,that means when press back button it will
 * exit the app,so we add a dialog to notify user whether exit app or not
 */
@Override
public void onBackPressed()
{
    int size=history.size();
    if ( history.size()>= 2) 
    {
        history.remove(size-1);
        setContentView(history.get(size-2));
    }
    else
    {
        if(dialog==null)
        {
            createDialog();
        }
        dialog.show();
    }   
}  
}  

在B.class:

     public void nextPage()
     {
      Intent intent=new Intent(B.this,C.class);
      intent.putExtra("name", productAdapter.getName(position));
      intent.putExtra("id", productAdapter.getID(position));    
      B.this.getParent().startActivityForResult(intent,11);
     }

    /*
     * modify data in modifyItem
     */
      public void handleActivityResult(int requestCode,int resultCode,Intent data)
     {
      String price=data.getExtras().getString("price");
      String name=data.getExtras().getString("name");
      String quantity=data.getExtras().getString("quantity");
      productAdapter.setName(name, modifyItem);
      productAdapter.setPrice(price, modifyItem);
      productAdapter.setQuantity(quantity, modifyItem);
      productAdapter.notifyDataSetChanged();
   }
C.class中的

@Override
public void onBackPressed() 
{
    if(price!=null)
    {
        Bundle bundle=new Bundle();
        bundle.putString("name",name);
        bundle.putString("price",price);
        bundle.putString("quantity",quantity);

        this.getParent().setResult(RESULT_OK,new Intent().putExtras(bundle));
        this.finish();
        Log.e("C","inner");
    }
    Log.e("C","outer");
    this.getParent().onBackPressed();
}

2 个答案:

答案 0 :(得分:0)

为什么叫完?向后压意味着它会破坏

答案 1 :(得分:0)

来自ActivityGroup的活动不会直接获得响应调用。 您需要从ActivityGroup重定向。 请看下面的答案 这是解决方案。请试试这个

https://stackoverflow.com/a/15047518/1403112