0

I'm making an app that shows another activity when I correctly login however in the home screen when I pressed back and then returned to the app the login activity showed.

I was able to avoid this overriding the onBackPressed this way:

public void onBackPressed()
        moveTaskToBack(true);
    }

Is this the best way to do it? Is there a more proper way to keep the state of the application when I exit it?

ALopez
  • 121
  • 1
  • 13
  • Could you explain it a bit more?! – Masked Man Oct 09 '16 at 21:47
  • @MaskedMan What is the "standard" way to handle this? I get it working overriding onBackPressed() and killing the login activity with finish(), but I feel something odd about it as if I'm not considering something and later I going to have issues with this. – ALopez Oct 09 '16 at 22:41
  • Are you saying if you are in the app, log in, back out of the app again, you want the home screen to show but the login screen does? You sound like you need a splash screen that is your launcher and handle your intent on launch from there. – zgc7009 Oct 09 '16 at 23:42
  • Thank you sou much that it's great idea I've never thought of it; that way the launch screen would works as a manager. Do you know if there is there a standard or recommended way to manage activities? – ALopez Oct 09 '16 at 23:55
  • add android:noHistory="true" to the Activity you want to not track – Zar E Ahmer Oct 10 '16 at 05:35
  • You would use a splash page that determines which page to load after a predetermined duration. Look up Android splash page in a web search and you will find a good number of examples. – zgc7009 Oct 10 '16 at 15:26

3 Answers3

0

In your login activity after starting intent call finish()

finish destroy your activity and avoid to run it again automatically.

FarshidABZ
  • 3,860
  • 4
  • 32
  • 63
0

Your issue is that you kept LoginActivity in stack. so when you press back it will kill MainActivity(after login) and since LoginAcitivity still there. it will be shown again. best is to kill LoginAcitivty after starting new activity.

call this in LoginActivity:

Intent intent = new Intent(context, MainActivity.class);
startActivity(intent);
finish();
Maher Abuthraa
  • 17,493
  • 11
  • 81
  • 103
  • I did that at first but even when I put finish() the Login class y still have to override the onBackPressed to get it working. But my question is: Is this the best way to handle this? Is there something I'm not taking care of and could give me problems later? – ALopez Oct 09 '16 at 22:34
  • It'd better to provide your code so we can help you better . you cannot get back a finished activity under any circumstances standardly. – Maher Abuthraa Oct 09 '16 at 22:49
  • I did exactly what you posted but when I press back in the HomeScreen Activity(the one that opens when the login is correct) and open the app again the login activity opens unless I override the onBackPressed() method. So I get it working this way but, Is this the best way to handle this? – ALopez Oct 09 '16 at 23:10
  • If you finish LoginActivity ( not fragment ! ) .. you cannot get back to it. if you are using fragments not activities... its quietly different – Maher Abuthraa Oct 09 '16 at 23:50
0

As you question I understand, you want to clear the entire history stack and start a new activity. For this, in API level 11 a new Intent Flag was added which is: Intent.FLAG_ACTIVITY_CLEAR_TASK

Just to clarify, use this:

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

For API <=10 now one can use IntentCompat class for the same. One can use IntentCompat.FLAG_ACTIVITY_CLEAR_TASK flag to clear task. So you can support pre API level 11 as well.

Shahinoor Shahin
  • 685
  • 3
  • 14