保证Activity启动时每次都调用create

原文:https://stackoverflow.com/questions/41766547/run-oncreate-every-time-android-app-is-opened

If you want only onCreate() to be called everytime, then you must remove from recents when home is pressed. For that you need to make changes in Manifest fie for the activity.Try using the below for the activity inManifets :

android:clearTaskOnLaunch="true"

android:finishOnTaskLaunch="true"
  • I will try number 2. Number 1 is not an option as the code in onResume does not work the way it does in onCreate (same code). – Laura Jan 20 '17 at 21:57
  •  
    Yes. It will work..!! Hope it helped. – Aparna Venkata Jan 21 '17 at 1:25
  •  
    Unfortunately, this does not work for me. – Laura Jan 23 '17 at 9:54
  • 1
    Hi @Laura, just try using "android:noHistory="true" in you manifest under that activity. This will for sure work. – Aparna Venkata Jan 23 '17 at 16:22
  •  
    In combination with clearTaskOnLaunch and finishOnTaskLaunch or not? I will try this tomorrow, thank you. – Laura Jan 24 '17 at 7:33
  •  
    Yes. Let me know if it worked – Aparna Venkata Jan 25 '17 at 8:04
  •  
    This worked for me! OnCreate is executed every time. Thank you! – Laura Jan 25 '17 at 9:34
  •  
    If only this worked with an activity that was started for result... – Laura Jan 25 '17 at 12:49
  •  
    It will work for any activity. Declare in Manifest for the next activity that is started for result. – Aparna Venkata Jan 25 '17 at 13:41
  •  
    I worked around this problem. Android documentation: Whether or not the activity should be removed from the activity stack and finished (its finish() method called) when the user navigates away from it and it's no longer visible on screen — "true" if it should be finished, and "false" if not. In this case, onActivityResult() is never called if you start another activity for a result from this activity.... So that is why– Laura Jan 25 '17 at 14:20 
原文地址:https://www.cnblogs.com/albert1017/p/8477567.html