调用finishAffinity()不会破坏Android应用程序或活动。即使重新启动应用,活动的数据仍然存在 [英] Calling finishAffinity() does not destroy android app or activity. Activity's data still persists even when app is restarted

查看:1257
本文介绍了调用finishAffinity()不会破坏Android应用程序或活动。即使重新启动应用,活动的数据仍然存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个巨大的Android编程问题/错误。

This is a huge Android Programming issue/bug.

调用 finishAffinity()不会关闭我的应用程序。

这可以从以下事实得到证明:


1.即使应用程序从屏幕上消失后,Android Studio调试器仍然显示应用程序正在运行,我又回到了手机主屏幕。



2.当'重新启动'应用程序时,初始活动的数据成员的值保持不变在应用程序假定'关闭'之前。



3.如果我在之后直接调用 System.exit(0) finishAffinity()然后关机正常工作,数据成员重置为初始默认值。




甚至更大的含义这个问题!在我开始另一个 ActivityB 之后,当我在 ActivityA 中调用完成()时,如果我回到 ActivityA ,那么数据成员尚未重置为默认值仍为旧值。


$
因为我来自C ++,当一个类被销毁然后它是 ACTUALLY <这是所有混淆 / strong>销毁并且与之关联的所有内存都已完全释放。



当切换到新活动或尝试退出应用程序时,获取活动以完全删除自身似乎是不可能的。

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 ActivityA after starting another ActivityB, if I ever go back to ActivityA then the data members have not been reset to default are still the old values.

This is ALL confusing to me since I've come from C++ where when a class is destroyed then it is ACTUALLY destroyed 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.

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


推荐答案

finishAffinity()不用于关闭应用程序 。它用于从当前任务中删除属于特定应用程序的许多活动(可能包含活动 s属于多个应用程序)。

finishAffinity() is not used to "shutdown an application". It is used to remove a number of Activitys belonging to a specific application from the current task (which may contain Activitys belonging to multiple applications).

即使您在应用程序中完成了所有活动,操作系统托管您的应用程序的进程不会自动消失(就像您调用 System.exit()时那样)。 Android最终会在遇到它时终止你的进程。你无法控制它(这是有意的)。

Even if you finish all of the Activitys 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).

如果你有一个调试器附加到进程,这也可以防止进程被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.

您谈到数据成员没有被清理,并且您声称这在C ++中的工作方式不同。实际上,那不是真的。您的数据成员被声明为 static 。它们不是实例变量,而是类变量。它们只存在一次(不是在类的每个实例中),它们是在加载类时创建和初始化的,并且它们在卸载类之前永远不会被销毁(在Android上永远不会发生)。 C ++具有完全相同的行为。

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. The 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.

您可以尝试使用实例变量而不是类变量来解决您的问题。

You might try using instance variables instead of class variables to solve your problem.

这篇关于调用finishAffinity()不会破坏Android应用程序或活动。即使重新启动应用,活动的数据仍然存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆