Clear the stack and exit the application onBackPressed ()

Basically my application has a loginScreen and after registering you can go through many steps. when i press the home button, the app goes in the background and if the user doesn't open it for a certain amount of time, the user session is closed and u goes back to the loginScreen. now the problem is that if I want to close the application from loginScreen, as soon as my session has expired, I press the back key and it should close, but it is not. this brings me to the previous item on the stack.

the wired thing is that on all methods onBackPressed () and when i start new intent i always used intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

and on loginScreen onBackPressed () i call finish()

but it doesn't work. does anyone know why? and how can i solve this problem.

Thanks for the help!!!

snippets of code 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 loginActivity:

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

      

+1


source to share


3 answers


You can do this in two ways:

  • kill application using android.os.Process.killProcess(android.os.Process.myPid());

    on backpress.

for this you need to add below permission to manifest.

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

      



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

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

And then write below code in each action of onResume () method

if(isKill)
{
    finish();
}

      

+4


source


When starting an activity that I don't want to include in the BackStack, I use:

myintent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

      



I read (around here) about onBackPressed () not calling, and they used onKeyPressed instead and specified a Back button. I'll see if I can find an example.

Here's an example: Android - onBackPressed () doesn't work

0


source


@Override
public void onBackPressed() {

   AlertDialog.Builder builder = new AlertDialog.Builder(this);
   builder.setTitle("Exit");
   builder.setMessage("Do you want to exit? ");
   builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {

 // do something when the button is pressed
       public void onClick(DialogInterface arg0, int arg1) {

           finish();
       **YourActivityName**.super.finishAffinity();
    }
 })
 .setNegativeButton("No", new DialogInterface.OnClickListener() {

// do something when the button is pressed
        public void onClick(DialogInterface arg0, int arg1) {
         }
  }).show();


}

      

0


source







All Articles