Java 调用finishAffinity() 不会破坏Android 应用程序或活动。即使重新启动应用程序,活动的数据仍然存在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39583184/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calling finishAffinity() does not destroy android app or activity. Activity's data still persists even when app is restarted
提问by zdanman
This is a huge Android Programming issue/bug.
这是一个巨大的 Android 编程问题/错误。
Calling finishAffinity()does not shut down my application.
This is evidenced from the fact that:
1. The Android Studio Debugger still says 'Application is running' even after the app disappears from the screen and I'm back to the phones home screen.
2. When 'restarting' the application the values of data members of the inital Activity remain the same as they were before the application supposedly 'shutdown'.
3. If I call System.exit(0)directly after finishAffinity()then the shutdown works properly and data members are reset to their initial default values.
There are even bigger implications to this problem! When I call finish()within ActivityAafter starting another ActivityB, if I ever go back to ActivityAthen the data members have not been reset to default are still the old values.
This is ALL confusingto me since I've come from C++ where when a class is destroyed then it is ACTUALLYdestroyed and all memory associated with it is completely released.
Getting an activity to completely delete itself when switching to a new activity or trying to exit the application seems IMPOSSIBLE.
调用finishAffinity()不会关闭我的应用程序。
这可以从以下事实中得到证明:
1. 即使在应用程序从屏幕上消失并且我回到手机主屏幕之后,Android Studio 调试器仍然显示“应用程序正在运行”。
2. 当“重新启动”应用程序时,初始 Activity 的数据成员的值与应用程序“关闭”之前的值保持相同。
3. 如果我在finishAffinity()之后直接调用System.exit(0),那么关闭正常工作并且数据成员被重置为其初始默认值。
这个问题有更大的影响!当我打电话
完成()内ActivityA开始另一个后ActivityB,如果我回去ActivityA那么数据成员没有被重置为默认值仍然是旧值。
这是所有混乱给我,因为我来自C ++在当一个类被破坏则ACTUALLY破坏,与之相关联的所有内存完全释放。
在切换到新活动或尝试退出应用程序时,让活动完全删除自己似乎是不可能的。
public class Startup extends AppCompatActivity
{
private static int iStarted = 0;
............
@Override
protected void onActivityResult(int request, int result, Intent data)
{
super.onActivityResult(request, result, data);
if (request == RESULT_EULA_RETURNED)
{
// shutdown
finishAffinity(); // iStarted remains = 1
return;
}
}
..........
@Override
protected void onResume()
{
super.onResume();
// perform startup
// even when restarted this remains = 1
if (iStarted == 0)
{
iStarted = 1; // this will stay = 1 until the application is manually killed via the CLOSE ALL method or via the debugger
}
}
}
采纳答案by David Wasser
finishAffinity()
is not used to "shutdown an application". It is used to remove a number of Activity
s belonging to a specific application from the current task (which may contain Activity
s belonging to multiple applications).
finishAffinity()
不用于“关闭应用程序”。它用于Activity
从当前任务中删除一些属于特定应用程序的s(可能包含Activity
属于多个应用程序的 s)。
Even if you finish all of the Activity
s in your application, the OS process hosting your app does not automatically go away (as it does when you call System.exit()
). Android will eventually kill your process when it gets around to it. You have no control over this (and that is intentional).
即使您完成了Activity
应用程序中的所有s,托管您的应用程序的操作系统进程也不会自动消失(就像您调用 时那样System.exit()
)。Android 最终会在处理它时终止您的进程。你无法控制这个(这是故意的)。
If you have a debugger attached to the process, this can also prevent the process being killed by Android, as the debugger keeps active objects in the process.
如果您有一个调试器附加到进程,这也可以防止进程被 Android 杀死,因为调试器会在进程中保留活动对象。
You talk about "data members" not being cleaned up, and you claim that this works differently in C++. Actually, that's not true. Your "data members" are declared static
. They aren't instance variables, they are class variables. They exist only once (not in every instance of the class), they are created and initialized when the class is loaded, and they are never destroyed until the class is unloaded(which never happens on Android). C++ has exactly the same behaviour.
你谈到“数据成员”没有被清理,你声称这在 C++ 中的工作方式不同。事实上,事实并非如此。您的“数据成员”已声明static
。它们不是实例变量,它们是类变量。它们只存在一次(不是在类的每个实例中),它们在类加载时被创建和初始化,并且在类卸载之前它们永远不会被销毁(这在 Android 上永远不会发生)。C++ 具有完全相同的行为。
You might try using instance variables instead of class variables to solve your problem.
您可以尝试使用实例变量而不是类变量来解决您的问题。
回答by Jeffrey Blattman
Android has no concept of "shut down my application". There is only the Android Activity
life cycle. There is not a connection between the VM's object life cycle on the activity life cycle; Android is free to re-use your Activity
object instance across on create / destroy boundaries. In short, you can't assume Android will ever construct a new Activity
object to handle a life cycle event.
Android 没有“关闭我的应用程序”的概念。只有 AndroidActivity
生命周期。活动生命周期上VM的对象生命周期之间没有联系;Android 可以自由地Activity
跨创建/销毁边界重用您的对象实例。简而言之,您不能假设 Android 会构造一个新Activity
对象来处理生命周期事件。
You'll need to manage your own state. For example, maybe you want to clear them in onCreate()
so whenever you activity is re-created they are reset. Note that I don't presume to know what logic you want to apply to the problem, I'm just giving an example.
您需要管理自己的状态。例如,也许您想清除它们,onCreate()
因此每当您重新创建活动时,它们都会被重置。请注意,我并不想知道您想对问题应用什么逻辑,我只是举个例子。