System.exit(0)导致活动快速应用程序启动时退出 [英] System.exit(0) causes the Activity quit quickly when the app starts

查看:214
本文介绍了System.exit(0)导致活动快速应用程序启动时退出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个问题, System.exit(0)使活动迅速退出时,应用程序启动:

I met a problem, System.exit(0) cause the Activity quit quickly when the app start:

在一个项目中,我看到使用 System.exit(0)来实现应用程序的完全退出,我这样做,但活动没有办理它的生命周期中的项目,所以我用下面的方法:

in a project, i see that use System.exit(0) to realize the complete exit of app, and i do that, but the activity did not go through its life cycle in project, so i use the following methods:

public class App extends Application {
    private int mLock = 0;
    @Override
    public void onCreate() {
        registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
                mLock++;
            }
            @Override
            public void onActivityDestroyed(Activity activity) {
                try {
                    //Simulation Activity Destroyed too time consuming and increase the probability of problems

                    Thread.sleep(300);
                } catch (InterruptedException e) {
                }
                mLock--;
                if(mLock<=0){
                    exit();
                }
            }
            @Override
            public void onActivityStopped(Activity activity) {}
            @Override
            public void onActivityStarted(Activity activity) {}
            @Override
            public void onActivitySaveInstanceState(Activity activity, Bundle outState) {}
            @Override
            public void onActivityResumed(Activity activity) {}
            @Override
            public void onActivityPaused(Activity activity) {}

        });
    }

    public void exit() {
        System.exit(0);
    }

}

但是,这样,出现问题,如果我退出的最后一项活动(退出应用程序),并立即和快速启动应用程序,然后退出活动,但它并没有完全启动(闪存退出)。

but, such, the problem arise, if i quit the last activity (quit app), and immediately and quickly start app, then Activity quit but it did not start complete (flash quit).

修改
我想要做的就是退出应用程序时,所有活​​动都destroyed.but什么,有一个问题:当应用程序要完成自己的过程中,所有活动被破坏,但还没有呼叫系统。退出(),推出了活动,则调用System.exit(),此时会出现一个闪光退出,如何避免这个问题?

edit what i want to do is quit app when all activity is destroyed.but,there is a problem:when app want to finish its own process,all activity are destroyed,but when haven't even call System. Exit (),launched a activity,then call System.exit(),at this time there will be a flash quit,How to avoid this problem?

好了,但我的目的是要结束应用程序进程(有的需要,而不是考虑的是它应该做的),只是在默认启动活动后销毁,而不是任何其他活动做这个手术结束过程中,我用上面的方法,以确保所有的启动活动将能够完成其生命周期中的应用程序。但是,有问题的描述,这应该是多线程的同步问题,但我睡在应用程序,为什么它可以在睡眠时创建的活动?别应用和活性不相同的线程,但是它们的线程ID是1

Well, but my purpose is to end the application process (some needs, not consider is it should do), just in the after of the default startup activity destroy, rather than in any other activity to do the end of this operation process, I use the above method in order to ensure that all start activity will be able to complete its life cycle in the app. But that had the problem as description, this should be multithreaded synchronization problem, but I have sleep in the application, why it can created activity during sleep? Don't application and activity is not the same thread, but their thread id is 1.

推荐答案

永远不要使用System.exit(0)。这违背了Android的编码实践。它的设计要立即退出应用程序,它是不是你想要的。如果用户presses家或后退,该活动将会从堆栈中弹出,所有这些生命周期方法将被调用。另一种方法是,如果你有一个理由离开应用程序pre-成熟使用完成()方法。

Do not ever use System.exit(0). That goes against the Android coding practices. It's designed to quit the app immediately which is not what you want. If the user presses "Home" or "Back", the Activity will be popped from the stack and all those lifecycle methods will be called. An alternative is to use the finish() method if you have a reason to leave the app pre-maturely.

这篇关于System.exit(0)导致活动快速应用程序启动时退出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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