导航到期望额外的活动

时间:2013-06-21 14:36:08

标签: android

我的应用程序中有活动A,B和C,它们按顺序流动。如果我在C中实现Up按钮,我希望它返回到B. Eclipse生成的代码存根是这样的:

@Override
public boolean onOptionsItemSelected(MenuItem item) 
{
    switch (item.getItemId())
    {
        case android.R.id.home:
            // This ID represents the Home or Up button. In the case of this
            // activity, the Up button is shown. Use NavUtils to allow users
            // to navigate up one level in the application structure. For
            // more details, see the Navigation pattern on Android Design:
            //
            // http://developer.android.com/design/patterns/navigation.html#up-vs-back
            //
            NavUtils.navigateUpFromSameTask(this);
            return true;
    }

    return super.onOptionsItemSelected(item);
}

但是,B期望在其onCreate方法中从A传递额外内容。 B和C之间的关系是C允许用户设置一些影响B显示方式的设置。我目前正在处理在onPause()中保存C中的更改。当用户按下Up而不是调用navigateUpFromSameTask(this)时,我应该完成()C吗?

2 个答案:

答案 0 :(得分:8)

如果您要从C返回B,如果您使用标准启动模式,您的活动将再次。 onSaveInstanceState将not (reliably) work

将活动B的启动模式声明为:

android:launchMode="singleTop"
如果您想返回活动,请在AndroidManifest.xml中

。请参阅docuexplanation here

答案 1 :(得分:0)

我相信你遇到与this类似的问题?基本上,如果你要从C回到B,那么它有可能需要再次调用onCreate()。这意味着您从意图中获得的额外内容将会消失,因此您必须使用onSaveInstanceState之类的内容存储它们。

相关问题