2

I am using LogOut menuitem in navigation drawer.The flow of an app is as follows,

  1. SplashScreen
  2. LogInActivity
  3. ShopList Fragment (Inside of Activity3)
  4. MainActivity (Here I am having LogOut menu item in Navigation Drawer).

If I press LogOut,I have written code to navigate LogInActivity.

But it moves to LogOut-->LogInActivity-->ShopList-->LogInActivity

LogOut Code is as follows,

if(id == R.id.nav_logout) {

            commonUtil.dbUtil.open();
            commonUtil.dbUtil.LogOut();
            Intent moveToMain = new Intent(context, LogInActivity.class);
            moveToMain.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
            moveToMain.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
            moveToMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(moveToMain);
            MainActivity.this.finish();

        }

Worked fine: (After changed setFlags to addFlags)

if (id == R.id.nav_logout) {

            commonUtil.dbUtil.open();
            commonUtil.dbUtil.LogOut();
            Intent moveToMain = new Intent(context, LogInActivity.class);
            moveToMain.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
            moveToMain.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
            moveToMain.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(moveToMain);
            MainActivity.this.finish();

        }
Parama Sudha
  • 2,583
  • 3
  • 29
  • 48
  • 1
    There is a similar question [here](http://stackoverflow.com/questions/12947916/android-remove-all-the-previous-activities-from-the-back-stack) with an answer. – Nsubuga Apr 04 '16 at 12:58

3 Answers3

1

Try to change setFlags to addFlags

Anton Shkurenko
  • 4,301
  • 5
  • 29
  • 64
1

This will clear all previous activities in the stack

Intent moveToMain = new Intent(context, LogInActivity.class);
moveToMain.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(moveToMain);
Tom Sabel
  • 3,935
  • 33
  • 45
1

Try this,

Intent moveToMain = new Intent(context, LogInActivity.class);
moveToMain .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |  Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(moveToMain);
Pradeep Gupta
  • 1,770
  • 1
  • 9
  • 23