1

我使用带有一些选项卡的 TabActivity,每个选项卡都包含一个 ActivityGroup,每个 ActivityGroup 管理多个 Activity。一个 ActivtyGroups 有三个 Activity: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 类中:

     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();
}
4

2 回答 2

0

为什么叫finish?压回去意味着它会破坏

于 2011-11-19T07:24:54.197 回答
0

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

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

于 2014-09-17T12:06:55.287 回答