使用 navigateUpFromSameTask() 从活动返回 [英] Returning from an activity using navigateUpFromSameTask()

查看:14
本文介绍了使用 navigateUpFromSameTask() 从活动返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个活动,A 和 B.当活动 A 首次启动时,它访问传递给它的 Intent(因为 Bundlenull,因为它应该是第一次通过),并相应地显示信息:

I have two activities, A and B. When activity A is first started, it accesses the Intent passed to it (because the Bundle is null, as it should be the first time through), and displays information accordingly:

CustInfo m_custInfo;
...
protected void onCreate(Bundle savedInstanceState)
{
    ...
    Bundle bundle = (savedInstanceState == null) ? getIntent().getExtras() : savedInstanceState;
    m_custInfo = (CustInfo) m_bundle.getSerializable("CustInfo");
    if (m_custInfo != null
        ...
}

这第一次运行正常.EditText 控件和 ListView 已正确填写.

This works fine the first time through. The EditText controls and ListView are filled out correctly.

现在,当点击列表中的一个项目时,活动 B 开始显示详细信息:

Now, when an item in the list is clicked, activity B is started to show the details:

m_custInfo = m_arrCustomers.get(pos);

Intent intent = new Intent(A.this, B.class);
intent.putExtra("CustInfo", m_custInfo); // CustInfo is serializable
// printing this intent, it shows to have extras and no flags

startActivityForResult(intent, 1);

就在活动 B 启动之前,框架调用 A 的重写 onSaveInstanceState():

Right before acivity B is started, the framework calls A's overridden onSaveInstanceState():

protected void onSaveInstanceState(Bundle outState)
{
    super.onSaveInstanceState(outState);

    outState.putSerializable("CustInfo", m_custInfo);
}

在活动 B 中,当在操作栏中按下向上按钮时,我想返回活动 A 并使其处于与之前相同的状态:

In activity B, when the Up button is pressed in the action bar, I want to return to activity A and have it be in the same state as it was before:

public boolean onOptionsItemSelected(MenuItem item)
{
    if (item.getItemId() == android.R.id.home)
    {
        Intent intent = NavUtils.getParentActivityIntent(this);
        // printing this intent, it shows to have flags but no extras

        NavUtils.navigateUpFromSameTask(this); // tried finish() here but that created an even bigger mess
        return true;
    }
    ...
}

这就是问题所在,当第二次在活动A的onCreate()中时,Bundle参数为null 并且 getExtras() 返回 null.由于 onSaveInstanceState() 被调用,我本来希望 Bundle 参数是非 null.

Herein lies the problem, when in onCreate() of activity A the second time, the Bundle parameter is null and getExtras() returns null. Since onSaveInstanceState() was called, I would have expected the Bundle parameter to be non-null.

我已在其他网站上阅读过有关此问题的信息,也尝试了这些建议,但没有任何效果.

I've read about this issue on other web sites, have tried the suggestions, but nothing works.

推荐答案

如果你希望你的应用程序以这种方式做出反应,你应该将你的 Activity A 的启动模式声明为:

If you want your application to react this way, you should declare the launch mode of your activity A as:

android:launchMode="singleTop"

在您的 AndroidManifest.xml 中.

in your AndroidManifest.xml.

否则android使用标准启动模式,这意味着

Otherwise android uses standard launch mode, which means

"系统总是在目标任务"

"The system always creates a new instance of the activity in the target task"

并重新创建您的 Activity(请参阅 Android 文档).

and your activity is recreated (see Android documentation).

如果使用 singleTop,系统会返回到您现有的活动(带有原始的额外活动),如果它位于任务的后堆栈的顶部.在这种情况下不需要实现 onSaveInstanceState.

With singleTop the system returns to your existing activity (with the original extra), if it is on the top of the back stack of the task. There is no need to implement onSaveInstanceState in this situation.

savedInstanceState 在您的情况下为 null,因为您的活动之前没有被操作系统关闭.

savedInstanceState is null in your case, because your activity was not previously being shut down by the OS.

注意(感谢安卓开发者指出这个):

Notice (thanks, android developer for pointing to this):

虽然这是一个问题的解决方案,但如果返回的活动不在在返回堆栈的顶部,它将不起作用.考虑活动A启动B的情况,也就是启动C,并且C的父活动被配置为A.如果您从 C 调用 NavigateUpFromSameTask(),活动将被重新创建,因为 A 不在堆栈顶部.

While this is a solution to the question, it will not work if the activity one returns to is not on the top of the back stack. Consider the case of activity A starting B, which is starting C, and C's parent activity is configured to be A. If you call NavigateUpFromSameTask() from C, the activity will be recreated, because A is not on top of the stack.

在这种情况下,可以使用这段代码:

In this case one can use this piece of code instead:

Intent intent = NavUtils.getParentActivityIntent(this); 
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP); 
NavUtils.navigateUpTo(this, intent);

这篇关于使用 navigateUpFromSameTask() 从活动返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆