简体   繁体   中英

Android prevent restart activity after startActivity

I have a problem with startActivity() methods.

Problem Structure

  1. Click Link (example, http://google.com ) in some TextView, using LinkMovementMethod
  2. Callback into LinkCatcher class (because some TextView exist almost all activity, so i using outer class)
  3. Post event to MainActivity using Otto (because it prevent startActivity outside of activity problem.)
  4. on MainActivity, intent to Activity B without any Flag, just one extra (link)
  5. When Activity B created, Activity A will destroy and call onCreate(Bundle savedInstanceState) (i insert some Log.e to my Code, definitely it re-call Log code)

Tried Methods

  1. Insert android:launchMode="singleInstance" in MainActivity, Activity B
  2. Insert android:configChanges="orientation|screenSize|keyboardHidden" in MainActivity (it means, orientation is not problem. i already implement onSaveInstanceState in all activity of my Application) 3.remove Step 3. instead of Post event to MainActivity, call startActiivty() in LinkCatcher Class

I tried all methods which i try it. and i don't know why activity is destroy and re-call onCreate (not onResume) even i don't use any finish() methods.

Because that's how Android is designed- at any moment an Activity not currently on screen can be killed. You can't prevent it. What you can do is account for it- you can save off any necessary data in onSaveInstanceState, and restore it in onCreate from the Bundle or in onRestoreInstanceState.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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