繁体   English   中英

返回活动时丢失Intent额外内容

[英]Losing Intent extras when returning to Activity

我有一个流程大致的应用程序:

  • 启动主要活动,输入一些数据
  • 启动活动A,在Intent中传递单个字符串
  • 活动A使用String并为用户提供一些选项
  • 有时使用将启动活动B(在背景中留下A)
  • 当用户从活动B返回时,重新创建活动A(调用onCreate); 我假设它已被删除以节省内存
  • getIntent()返回的intent不会将数据传递给Activity A的原始实例

我知道第一次调用时数据是存在的,因为如果不是,应用程序就会死掉。

我知道当用户返回活动A(通过'后退'键)时数据不存在,因为应用程序死了(来自onCreate()的extras.getString(...)的空指针)。

这是预期的行为吗?

我应该在onSaveInstanceState()中保存extras包的内容吗?

编辑:下面的代码片段:

主要活动使用以下内容启动新活动:

Intent i = new Intent(a, BookISBNSearch.class);
i.putExtra(BookISBNSearch.BY, "isbn");
a.startActivityForResult(i, R.id.ACTIVITY_CREATE_BOOK_ISBN);

BookISBNSearch中的onCreate读取:

...
Bundle extras = getIntent().getExtras();
mIsbn = extras.getString("isbn");
String by = extras.getString(BY);
...

稍后使用它:

if (mIsbn != null) {
    ....do some stuff....
} else if (by.equals("isbn")) {
    ....do some other stuff....

用户可以从BookISBNSearch开始另一个活动。 启动新活动的代码是:

/*
 * Start scanner activity.
 */
private void startScannerActivity() {
    if (mScannerIntent == null) {
        mScannerIntent = new Intent("com.google.zxing.client.android.SCAN");
    }
    if (!mScannerStarted) {
        mScannerStarted = true;
        startActivityForResult(mScannerIntent, ACTIVITY_SCAN);
    }
}

用户运行扫描程序,执行大量其他操作,最后按“后退”键返回此活动。 在这一点上,他们得到了下面列出的崩溃。 相关部分:

at com.eleybourn.bookcatalogue.BookISBNSearch.onCreate(BookISBNSearch.java:142)

对应于这一行:

} else if (by.equals("isbn")) {

我从中得出结论'by'是空的。

此外,还有其他可能的代码路径启动表现出相同问题的其他(非外部)活动。 即使打开了自动删除活动,也不会在我的手机中或AVD中发生这种情况。

它只发生在一小部分用户身上。

java.lang.NullPointerException
at com.eleybourn.bookcatalogue.BookISBNSearch.onCreate(BookISBNSearch.java:142)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1050)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
at android.app.ActivityThread.access$1500(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3691)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:670)
at dalvik.system.NativeStart.main(Native Method)

尝试将您的Intent数据写入savedInstance!

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if(getIntent() != null) {
        // Activity was started and got an Intent with data
        ...
    } else if(savedInstanceState != null) {
        // No intent is available, try getting data from savedInstance
        ...
    }

}

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

    if(outState != null) {
        // Write here your data
        ...
    }
}

我希望这可以帮到你 :)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM