1

I have activities [A] [B] [C] in my Android Apps. - [A] is LoginActivity - [B] is DashboardActivity - [C] is InventoryListActivity

in each [B] and [C] activity, theres is a logout button with code :

public void doLogout(){
        // clear all preferences

    // Return to the login activity
    Intent intent  = new Intent(getBaseContext(), LoginActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);        
    startActivity(intent);

    finish();
}

When i try to execute logout button in [C] activity with this flow [A] --> [B] --> [C], it does go to LoginActivty, but when i press back button it goes to [B] activity. What i want is if back button pressed i want to go Android Home Screen.

Please advise, how to destroy all activity when logout function executed.

NeoBeezz
  • 11
  • 1
  • 3
  • for that you have to finish that activity when you are moving from activity a ==>b , finish activity a. From B==> C finish B. But in that case you have to manage Back button. If user click back button of activity B, then start new activity to go to activity A while finish activity B. – Sumant Jan 16 '12 at 13:16
  • a=>b i'm already using finish activity a. but from b=>c i dont use finish activity b because i want still if user press back button in c activity it able to go to b activity. it there tricky code instead of using finish activity in all activity ? – NeoBeezz Jan 16 '12 at 13:27
  • Just Check out this link detailed explanation is given : http://stackoverflow.com/questions/6330260/finish-all-previous-activities – mayur rahatekar Jan 16 '12 at 13:39
  • [Try out this](http://stackoverflow.com/a/11430255/940096) also – Praveenkumar Jul 27 '12 at 04:29

2 Answers2

3

For android phones before 2.0 , u can use this : override it in your activity

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)  {
    if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
        // do something on back.
       Intent intent  = new Intent(getBaseContext(), LoginActivity.class);
     intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);        
     startActivity(intent);

        finish();


        return true;
    }

    return super.onKeyDown(keyCode, event);
}

For above 2.0 , u can override this to detect backpress and do your task :

@Override
public void onBackPressed() {
// do something on back.
 Intent intent  = new Intent(getBaseContext(), LoginActivity.class);
         intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);        
         startActivity(intent);

            finish();
return;
}

hope it helps you

Pratik Bhat
  • 7,166
  • 2
  • 35
  • 57
-3

Try

System.exit(0);

Or

System.exit(1);

Edit

Ignore above solution

You can use custom broad cast receivers to perform logout functionality. You need to follow this link:

http://androidprogrammers.wordpress.com/2011/02/23/broadcastreceivers-and-context-sendbroadcast/

Awais Tariq
  • 7,724
  • 5
  • 31
  • 54