0

My app begins with Activity A (log in screen) which I always want on the bottom of the Activity stack.

Based on some logic after login, I launch some other Activity B-Z. There is a menu in the app which allows you to switch around between any Activity B-Z.

If the user hits the back button enough times, I don't want them returned to the login screen. I want the user to be sent to the Android Home screen if the back button is pressed on the Activity which has Activity A as the next Activity on the stack. This Activity is not guaranteed to be the Activity which was launched by Activity A because my Activities use singleTop.

Ideas?

The only other option I can think of is to remove singleTop and whatever Activity is launched by Activity A could remember that (my Activities all derive from a base class, which I would use to do that).

Another possibility may be to do something like the following in the onBackPressed handler:

if (getParent().getClass().getName().equals(ActivityA.class.getName())) {}

Andrew
  • 20,756
  • 32
  • 99
  • 177

2 Answers2

1

Although not a direct answer to your question, but if the problem is that

I don't want them returned to the login screen

then the classic solution is to finish() the login Activity when the user has logged in successfully. This way you'll make sure the user will never return to that Activity.

Egor
  • 39,695
  • 10
  • 113
  • 130
  • My problem with this is I *want* ActivityA on the stack. Should a webservice in my app return saying the user is no longer logged in, I launch ActivityA with the flag `FLAG_ACTIVITY_CLEAR_TOP`. Since ActivityA is on the bottom of the stack, it destroys all Activities on top and returns the user to ActivityA (log in screen). I don't know if I can accomplish the same thing without ActivityA already on the bottom of the stack, can I? – Andrew Jan 20 '14 at 21:18
  • 1
    @Andrew: `FLAG_ACTIVITY_CLEAR_TOP` will work even if the activity is not on the stack AFAIK, if you use `FLAG_ACTIVITY_SINGLE_TOP` as well (or, in your case, use `singleTop` everywhere, which is rather odd). – CommonsWare Jan 20 '14 at 21:33
  • Here's a link to a discussion that may help you solve your problem: http://stackoverflow.com/questions/3007998/on-logout-clear-activity-history-stack-preventing-back-button-from-opening-l?rq=1. As you'll see, there exists a clean solution, which unfortunately works only on API v11+, and a couple of clever hacks that work well on any Android. – Egor Jan 20 '14 at 21:33
  • Thanks for your responses guys. I'll do some reading. Unfortunately, I support back to version 8 because so many people are still using 2.2/2.3. – Andrew Jan 20 '14 at 21:34
  • 1
    @CommonsWare, FLAG_ACTIVITY_SINGLE_TOP also requires the Activity to exist on the stack and only does the job if that Activity is actually on top of the stack. So both FLAG_ACTIVITY_SINGLE_TOP and FLAG_ACTIVITY_CLEAR_TOP won't work if the Activity was previously destroyed. – Egor Jan 20 '14 at 21:36
  • So, if the Activity is required to be on the stack and I'm supporting version 8, what are my options? – Andrew Jan 20 '14 at 21:50
0

To do this, Why don't you get the User Login information, and store it in your apps private shared preferences. Then when the User launches your application, you can read the Shared preferences from activity A, and automagically log them in to activity b .

something like this:

 SharedPreferences myPrefs = getBaseContext().getSharedPreferences("MyPrefs", SharedPreferences.MODE_PRIVATE);

 if((myPrefs.getString("username",null) != null) && (myPrefs.getString("password",null) !=null)){

   // Make sure your user is a member of your application

   // auto log in to the home page 
   Intent bIntent = new Intent(getBaseContext(), BIntent.class);
   startActivity(bIntent);

 }

For more reference on how to use Shared Preferences

http://developer.android.com/reference/android/content/SharedPreferences.html

Then if you want to be really slick, Store the Class name when the onDestroy() method is called in each Activity, overwriting each class as the last open Activity. So whichever activity the user was last on before the application was closed is stored in preferences and you can read that in the login activity, then from there launch b-z

This will make it so that the login activity is always in memory, since it is first launched to check your users credentials.

kandroidj
  • 13,784
  • 5
  • 64
  • 76
  • Yes, but there is still a problem. Suppose the login Activity opens Activity2. Now, while on Activity2, the user hits the back button. If I know the Activity after the login Activity is always going to be Activity2, can call `moveTaskToBack(true)` in `onBackPressed` and the application will minimize. But I don't know which Activity will be the Activity following the login Activity. It can be any Activity. This is why I need to know how to determine this. – Andrew Jan 21 '14 at 20:22
  • in the End of my description i mention keeping track of the current View in `SharedPreferences`. If you do this , you can query the prefs for the Unique identifier for you activity and launch it. It is a little work to add sharedPrefs calls to each activity, but I am thinking this could be a valid option. – kandroidj Jan 22 '14 at 14:10