我使用带有一些选项卡的 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();
}