1

I want to perform a logout function in which I want to clear all the activities before the logout and start a new Login Activity

Here is my code

Utilities.logoutPlayerDefaults(Profile.this);
Utilities.vibrate(Profile.this);
Intent myIntent = new Intent (Profile.this,FBLogin.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(myIntent);

But it doesn't work. If i press back button i go back to Profile

Ram kiran Pachigolla
  • 20,897
  • 15
  • 57
  • 78
Muhammad Umar
  • 11,391
  • 21
  • 91
  • 193

2 Answers2

0

Try following way,

@Override
public void onBackPressed() 
{
   Intent myIntent = new Intent (Profile.this,FBLogin.class);
   myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
   myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
   myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
   startActivity(myIntent);
   super.onBackPressed();
}
Vigbyor
  • 2,568
  • 4
  • 21
  • 35
  • well you havent mentioned API Level in your question. – Vigbyor Jul 11 '13 at 04:49
  • back button is meant to work the way it is designed for. why would you override the functionality. you have action bar and navigation drawer to help you. – Raghunandan Jul 11 '13 at 04:55
  • @Raghunandan If i should press backBtn the app should close instead of showing last activities. I want to clear the Activity Stack at logout – Muhammad Umar Jul 11 '13 at 09:38
  • @MuhammadUmar, Why dont you use `finish()` method ? – Vigbyor Jul 11 '13 at 09:46
  • @MuhammadUmar use action bar on click on app icon navigate to home screen and press back button to exist. I had a long discussion on so with other developers and i went through the docs and i felt using action bar was right and back button is suppose to take you to the previous screen, also no where in the docs they suggest overriding back button functionality – Raghunandan Jul 11 '13 at 12:45
0

Change your code to below :

Utilities.logoutPlayerDefaults(Profile.this);
Utilities.vibrate(Profile.this);
Intent myIntent = new Intent (Profile.this,FBLogin.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(myIntent);
finish();
baldguy
  • 2,090
  • 1
  • 16
  • 25
  • @Muhammad Umar check my answer perhaps you are not calling finish in your activity so in pressing back button you are getting profile activity. – baldguy Jul 11 '13 at 05:12
  • Tried didn't work. Actually let us say we have MainMenu>Profile nOW If I logout to Profile and then do finish and then go to FBClass then only Profile get out of the way and if i press back i will go to MainMenu. Which is not what i want, i want the app to close if i go back i-e no Activity before FBLogin. – Muhammad Umar Jul 11 '13 at 15:16