Eliminate intent when returning to activity

I have an application with a stream roughly equal to:

  • Start the main activity, enter some data
  • Start operation A by passing one line to Intent
  • Action A uses String and provides the user with some options.
  • Sometimes use triggers Activity B (leaving A in the background)
  • When the user returns from Activity B, activity A is recreated (called onCreate); I assume it was deleted to save memory.
  • The target returned by getIntent () does not have data passed to the original instance of Activity A

I know that the data was in the first call, because the application will die if it is not.

I know that there is no data when the user returns to Activity A (via the back key) because the application is dying (null pointer from extras.getString (...) to onCreate ()).

Is this the intended behavior?

Should I save the contents of the package of additional components in onSaveInstanceState ()?

Edited: code snippets below:

The main activity launches a new action using:

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

onCreate in BookISBNSearch reads this:

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

and uses it a little later:

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

the user can start another action from BookISBNSearch. Code starting with a new action:

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

the user launches the scanner, does a bunch of other things, and finally presses the back key to return to this action. At this point, they get the crash indicated below. Relevant Part:

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

matches the line:

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

from which I conclude that 'by' is null.

In addition, there are other possible code paths that trigger other (non-external) actions that show the same problem. This does not happen on my phone, as well as in AVD, even with the automatic removal feature turned on.

This happens only to a small part of users.

 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) 
+7
source share
1 answer

Try writing your Intent data to the savedInstance file!

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

I hope this was helpful :)

+1
source

All Articles