简体   繁体   English

android 活动在调用完成()后继续运行

[英]android activity keep running after calling finish()

In splash activity I want to exit app when calling onStop() ( I want to stop app if user clicks home button , Close screen or press backbutton )在启动活动中,我想在调用onStop()时退出应用程序onStop()如果用户单击主页按钮、关闭屏幕或按后退按钮,我想停止应用程序

Unfortunately calling finish() didn't help , even after exiting app the splash activity continue working and even starts the next activity不幸的是,调用finish()没有帮助,即使在退出应用程序后,启动活动继续工作,甚至开始下一个活动

I would like to mention that there is an asynctask class inside splash.activity and there is also another class jsonfetch called in splash.activity ( fetch data from server and open next activity after ) I think this one doesnt stop because when exiting app it opens the next activity after few secondes我想提的是里面有一个splash.activity类的AsyncTask并且还有另一个类jsonfetch称为splash.activity(读取距离后,服务器和开放的下一个活动数据),我觉得这一个犯规停止,因为在退出应用程序时,它会打开几秒钟后的下一个活动

I suspect you are navigating to your MainActivity from you SplashActivity.我怀疑您是从 SplashActivity 导航到 MainActivity。

Try adding a global variable shouldNavigate to your SplashActivity and change it to false if onStop() is called;尝试将全局变量 shouldNavigate 添加到您的 SplashActivity 并在调用 onStop() 时将其更改为 false;

private boolean shouldNavigate = true

    @Override
    protected void onStop() {
        shouldNavigate = false;
        super.onStop();   
        finish();
    }

And then do a check when navigating to your MainActivity:然后在导航到您的 MainActivity 时进行检查:

if (shouldNavigate){
     // navigate to MainActivity
}

If you are using countdowntimer in splash activity, then please stop that in onStop() .如果您在启动活动中使用倒数计时器,请在onStop()停止它。 It will help you.它会帮助你。

After several tests I found that System.exit(0);经过几次测试,我发现 System.exit(0); fixed the problem for me , I hope this solution helps anyone that has the same issue为我解决了这个问题,我希望这个解决方案可以帮助任何有同样问题的人

try this尝试这个

System.exit(0);
try{ 
    finish();
    //code  press backbutton 
    }catch(Exception  e){
}
finally{  //inside  
    System.exit(0);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM