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

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

问题描述

我有两个活动,A和B.当活性的首次启动,它将访问意图传递给它(因为捆绑,因为它应该通过在第一时间),并相应地显示信息:

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);

acivity B的启动权之前,该框架调用A的覆盖的onSaveInstanceState()

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

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

在活动B,当向上按钮pssed操作栏中$ P $,我想回到活性的,并让它在同一个国家,因为它以前:

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;
    }
    ...
}

在这里存在一个问题,当的onCreate()活动的第二次,在捆绑参数 getExtras()返回。由于的onSaveInstanceState()叫,我本来期望的捆绑参数为非 -

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.

推荐答案

如果你希望你的应用程序能够响应这种方式,你应该申报的活动为您的发射方式为:

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

android:launchMode="singleTop"

在你的Andr​​oidManifest.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"

和你的活动是重新创建(见的Andr​​oid文档)。

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,因为你的活动不是previously被关闭的操作系统。

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

的通知(感谢,Android开发者为指向这个

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

虽然这是一个解决问题的办法,就不会工作,如果活性的一种返回到就是的上背面堆栈的顶部。 考虑活动的情况下的起始B,其是起始物C,和C的父活动被构成为A. 如果你调用 NavigateUpFromSameTask()从C,该活动将被重新创建,因为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.

在这种情况下,可以利用这片code来代替:

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天全站免登陆