Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
723 views
in Technique[技术] by (71.8m points)

android - clean stack and exit app onBackPressed()

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();
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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();
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...