干净的堆栈和退出应用程序onBack pressed() [英] clean stack and exit app onBackPressed()

查看:215
本文介绍了干净的堆栈和退出应用程序onBack pressed()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我的应用程序有一个loginScreen并登录后U可以通过许多活动。当我preSS home键的应用程序进入后台,如果用户犯规一定的时间,用户关闭会话中打开它和u回到loginScreen。现在的问题是,如果我想从loginScreen关闭应用程序,一旦我的会话已过期我preSS返回键,它应该关闭,但它没有。它使我在堆栈中的previous元素。

有线的是,在所有onBack pressed()方法,并当过我开始新的意图,我总是用 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 并在loginScreen onBack pressed()我叫完成() 但它不工作。有谁知道为什么吗?以及如何解决这个问题。

感谢您的帮助!

code片段 在许多活动:

  @覆盖
    公共无效onBack pressed(){
        mpButtonClick.start();
        意向意图=新的意图(这一点,MenuPag​​ina.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(意向);
        super.onBack pressed();
    }
 

在loginActivity:

  @覆盖
    公共无效onBack pressed(){
        super.onBack pressed();
        。getIntent()setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        LoginActivity.this.finish();
    }
 

解决方案

您可以做双向的:

  1. 在使用杀应用程序 android.os.Process.killProcess(android.os.Process.myPid()); 背preSS

对于这一点,你需要添加下面的权限清单。

 <使用,许可
        机器人:名称=android.permission.KILL_BACKGROUND_PROCESSES/>
 

2。使用静态布尔'isKill变量,默认为false,每OnCreate中设置虚假的登录活动时间()方法。

设置isKill值为true在登录活动onBack preSS()方法。

然后下面写code。在每一个活动onResume()方法

 如果(isKill)
{
    完();
}
 

basically my app has a loginScreen and once logged u can go through many activities. when i press the home button the app goes in background and if the user doesnt open it within a certain amount of time, the users session closes and u return to the loginScreen. now the problem is that if i want to close the app from the loginScreen once my session has expired i press the back key and it should close but it doesnt. it brings me to the previous element in the stack.

the wired thing is that on all onBackPressed() methods and when ever i started new intents i always use intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); and on the loginScreen onBackPressed() i call finish() but it doesnt work. anyone know why? and how do i solve this problem.

Thanks for the help!!!

code snippets in many activities:

@Override
    public void onBackPressed() {
        mpButtonClick.start();
        Intent intent = new Intent(this, MenuPagina.class); 
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
        startActivity(intent); 
        super.onBackPressed(); 
    }

in the loginActivity:

@Override
    public void onBackPressed() {
        super.onBackPressed();
        getIntent().setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        LoginActivity.this.finish();
    }

解决方案

You can do this two way:

  1. kill app by using android.os.Process.killProcess(android.os.Process.myPid()); on back press.

for this you need to add below permission on manifest.

<uses-permission
        android:name="android.permission.KILL_BACKGROUND_PROCESSES" />

2 . use static boolean 'isKill' variable with default false and every time set false in login activity oncreate() method.

set isKill value true in login activity onBackPress() method.

And then write below code in every activity onResume() method

if(isKill)
{
    finish();
}

这篇关于干净的堆栈和退出应用程序onBack pressed()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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