Losing Intent extras when returning to Activity
<p>I have an application with a flow that is roughly:</p>

<ul>
<li>Start Main Activity, enter some data</li>
<li>Start Activity A, passing a single string in the Intent</li>
<li>Activity A uses the String and gives the user some options</li>
<li>Sometimes the use will start Activity B (leaving A in background)</li>
<li>When the user returns from Activity B the Activity A is recreated (onCreate is called); I assume it has been deleted to save memory</li>
<li>The intent returned by getIntent() does NOT have the data passed to the original instance of Activity A</li>
</ul>

<p>I know the data was there in the first call because the app would die if it was not.</p>

<p>I know the data is not there when the user returns to Activity A (via the 'back' key) because the app dies (null pointer from extras.getString(...) in onCreate() ). </p>

<p>Is this intended behaviour?</p>

<p>Should I be saving the contents of the extras bundle in onSaveInstanceState() ?</p>

<p>Edited: code snippets below:</p>

<p>Main Activity starts the new activity using:</p>

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

<p>onCreate in BookISBNSearch reads this:</p>

<pre><code>...
Bundle extras = getIntent().getExtras();
mIsbn = extras.getString("isbn");
String by = extras.getString(BY);
...
</code></pre>

<p>and a little later uses it:</p>

<pre><code>if (mIsbn != null) {
    ....do some stuff....
} else if (by.equals("isbn")) {
    ....do some other stuff....
</code></pre>

<p>the user may start another activity from BookISBNSearch. The code that starts the new activity is:</p>

<pre><code>/*
 * 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);
    }
}
</code></pre>

<p>the user runs the scanner, does a bunch of other stuff, and eventually presses the 'back' key to return to this activity. At which point they get the crash listed below. The relevant part:</p>

<pre><code>at com.eleybourn.bookcatalogue.BookISBNSearch.onCreate(BookISBNSearch.java:142)
</code></pre>

<p>corresponds to the line:</p>

<pre><code>} else if (by.equals("isbn")) {
</code></pre>

<p>from which I conclude that 'by' is null.</p>

<p>Further, there are other possible code paths starting other (non-external) activities that exhibit the same problem. This does not occur in my phone, nor in AVD even with auto-deleting activities turned on.</p>

<p>It only happens for a small proportion of users.</p>

<pre><code>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)
</code></pre>
