1

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

3 回答 3

4

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();
}
于 2012-05-03T09:13:57.180 回答
0

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

myintent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

I have read (on here somewhere) about onBackPressed() not calling and instead they used onKeyPressed and specified the back button. I'll see if I can find the example.

Here is the example: Android - onBackPressed() not working

于 2012-05-03T09:10:44.753 回答
0
@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();


}
于 2018-12-20T09:43:12.467 回答