这个问题很老,但是这个问题有一个答案,不需要 adb、Android Studio 等。唯一的要求是 API 23 或更高版本。
要通过操作系统模拟应用程序重启,请在应用程序运行时进入应用程序设置,禁用(然后您可以启用)权限并从最近的应用程序中返回应用程序。当权限被禁用时,操作系统会终止应用程序但保留保存的实例状态。当用户返回应用程序时,应用程序和最后一个活动(具有已保存状态)被重新创建。
“无后台进程”方法有时会导致相同的行为,但并非总是如此。例如,如果应用程序正在运行后台服务,则“无后台进程”不会执行任何操作。但是该应用程序可能会被包括其服务在内的系统杀死。即使应用程序有服务,权限方法也有效。
例子:
我们的应用程序有两个活动。ActivityA 是从启动器启动的主要活动。ActivityB 从 ActivityA 开始。我将只展示 onCreate、onStart、onStop、onDestroy 方法。Android 总是在调用 onStop 之前调用 onSaveInstanceState,因为处于停止状态的活动可能会被系统杀死。[ https://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle]
许可方式:
<start app from launcher first time>
Application onCreate
ActivityA onCreate WITHOUT savedInstance
ActivityA onStart
<open ActivityB>
ActivityB onCreate WITHOUT savedInstance
ActivityB onStart
ActivityA onStop (the order is like this, it is stopped after new one is started)
<go settings>
ActivityB onStop
<disable a permission>
//Application is killed, but onDestroy methods are not called.
//Android does not call onDestroy methods if app will be killed.
<return app by recent apps>
Application onCreate (this is the important part. All static variables are reset.)
ActivityB onCreate WITH savedInstance (user does not notice activity is recreated)
//Note that ActivityA is not created yet, do not try to access it.
ActivityB onStart
<return ActivityA by back>
ActivityA onCreate WITH savedInstance (user does not notice activity is recreated)
ActivityA onStart
ActivityB onStop
ActivityB onDestroy
<press back again, return launcher>
ActivityA onStop
ActivityA onDestroy
<open app again>
//does not call Application onCreate, app was not killed
ActivityA onCreate WITHOUT savedInstance
ActivityA onStart
我想比较其他答案中提到的其他方法。
不要保留活动:这不会杀死应用程序。
<start app from launcher first time>
Application onCreate
ActivityA onCreate WITHOUT savedInstance
ActivityA onStart
<open ActivityB>
ActivityB onCreate WITHOUT savedInstance
ActivityB onStart
ActivityA onStop
ActivityA onDestroy (do not keep)
<return launcher by home button>
ActivityB onStop
ActivityB onDestroy (do not keep)
<retun app from recent apps>
// NO Application onCreate
ActivityB onCreate WITH savedInstance (user does not notice activity recreated)
ActivityB onStart
<return ActivityA by back>
ActivityA onCreate WITH savedInstance (user does not notice activity recreated)
ActivityA onStart
ActivityB onStop
ActivityB onDestroy
<press back again, return launcher>
ActivityA onStop
ActivityA onDestroy
<open app again>
//does not call Application onCreate, app was not killed
ActivityA onCreate WITHOUT savedInstance
ActivityA onStart
强制停止方法:不存储保存的实例状态
<start app from launcher first time>
Application onCreate
ActivityA onCreate WITHOUT savedInstance
ActivityA onStart
<open ActivityB>
ActivityB onCreate WITHOUT savedInstance
ActivityB onStart
ActivityA onStop
<go settings>
ActivityB onStop
<force stop, return app from recent apps>
Application onCreate
ActivityA onCreate WITHOUT savedInstance
//This is important part, app is destroyed by user.
//Root activity of the task is started, not the top activity.
//Also there is no savedInstance.