3

I need this behavior in my app, "user must have to login in each time he tries to launch it(touching the app icon directly, or by task manager or via recent apps)" So the activities are in this order (think the App name is Foo app)

  1. S - Splash Screen
  2. L - Loading Screen
  3. Lo - Login Screen
  4. M - Main Menu (Has list of tasks)
  5. T - Task Screen

"User starts the app and proceeds as in the following order : S -> L -> Lo (logs in) -> M (selects one of the Tasks) -> Task Screen

  • so while he is in the Task Screen, he presses the Home button and the App goes to the background and he uses another app. and lunches the Foo app again. So in this if he pressed the Back button, it shows the previously being used Task Screen??? and if pressed Back button again, it goes to the Main screen again, and so forth...???

  • This should not happen, simply what I want is, when the App comes to foreground, user needs to login and never be able to go to back to any of the screens.

Note : all the screens have extended a BaseActivity class which has extended the Activity class. and in there I have used onResume(),onPause() method and another custom method to find out when the extended child class goes to pause,and resume when the app comes from the background!!! And in some Screen I have had to use Fragments too...!

Thanks in advance for your time and help!

Randika Vishman
  • 7,983
  • 3
  • 57
  • 80
  • Downvoted because of duplicates [here](http://stackoverflow.com/questions/3007998/on-logout-clear-activity-history-stack-preventing-back-button-from-opening-l/12468288#12468288) (a very good answer, btw), and [here](http://stackoverflow.com/questions/5794506/android-clear-the-back-stack) – verybadalloc Jun 14 '13 at 11:12
  • If the user pressed Home or some other activity came on top (the incoming dialer), then when the user returns to your activity (by application icon or back) then HE SHOULD BE EXPECTED to see your app where it was. I haven't seen a SERIOUS and POPULAR Android app to behave differently ... I don't see any reason why it should not behave as such – gunar Jun 14 '13 at 11:21
  • Thanks for the both comments at first. @verybadalloc Hey, I also so that answer before I posted this, but those things I have had implemented in my app, but didn't give the desired results! :( – Randika Vishman Jun 15 '13 at 14:36
  • @gunar Hi Gunar,if you can just imagine about an App, which you can hide or lock all of the images which you selects through the App, and for any time you need to access those images, you would have to login through the App, but not directly within the lastly viewed Photo gallery of that App. so in order to implement that kind of behavior we need the above mentioned scenario to be implemented – Randika Vishman Jun 15 '13 at 14:41
  • When the Activity backStack is as follows **A** -> **B** -> **C** -> **D** And when the user is in **D**, he presses Home button, In any of the ways he launches the app again later, I want him to get back to **A** activity, like simple pop back the Stack up to **A**!As it's the **Login Activity** I still can't approach this! I can find out in anywhere of the App, that when it's comes back from the Background to Foreground, and Pass an Intent to StartActivity method with FLAG_ACTIVITY_CLEAR_TASK and I've already been using **android:clearTaskOnLaunch="true" **, But still the problem is there! – Randika Vishman Jun 17 '13 at 06:36

2 Answers2

0

Add tag android:clearTaskOnLaunch="true" to root (main) activity tag in manifest. Then user will always return to this activity.

To jump to Login from any other activity, use:

public static void logoff(Context c){
    Intent logoff = new Intent(c,LoginActivity.class);
    logoff.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
    c.startActivity(logoff);
}
S.D.
  • 29,290
  • 3
  • 79
  • 130
  • Hi, Thanks for the reply, I guess I have done the same kind of thing, but that didn't work actually!Anyway what's the reason of creating a Static method? And when to call this? My question is actually I also do the same kind of thing, and successfully starts a new Login Activity or can go to the Splash Screen, but the problem is at that point if the User hits the Back button, he/she still can access the previous Activity on the backstack, which he was using at the last time he used it! :( (This behavior is needed, to avoid to provide the protection to the information within the app) Any help? – Randika Vishman Jun 15 '13 at 14:47
  • @Vishman The `logoff` method will do what it says, it will take user back to login activity from any other activity. And user can't undo this by pressing back, because it ends old task and starts a new one. This is static so it can be easily called from anywhere. Now, adding `android:clearTaskOnLaunch="true"` to Login activity in manifest will make sure that when user goes somewhere and returns to app, he comes to login activity and in a refreshed new task.(just like `logoff` methods) Also, I recommend use Fragments and place splash/loading/login fragments in Login Activity only. – S.D. Jun 16 '13 at 06:16
  • hi, thanks for your explanation, I will try this and inform you the results ok? – Randika Vishman Jun 17 '13 at 01:47
  • When the Activity backStack is as follows **A** -> **B** -> **C** -> **D** And when the user is in **D**, he presses Home button, In any of the ways he launches the app again later, I want him to get back to **A** activity, like simple pop back the Stack up to **A**!As it's the **Login Activity** I still can't approach this! I can find out in anywhere of the App, that when it's comes back from the Background to Foreground, and Pass an Intent to StartActivity method with FLAG_ACTIVITY_CLEAR_TASK and I've already been using **android:clearTaskOnLaunch="true" **, But still the problem is there! – Randika Vishman Jun 17 '13 at 06:36
  • I'm sorry guys but it's bit silly to say, however I found a half way solution to my question by getting the help of the following [answer](http://stackoverflow.com/a/5862048/651422) Hope this would be helpful to you all in case you met the same question. – Randika Vishman Jul 12 '13 at 04:23
0

I am not going to argue why your question describes a not so pleasant user-experience, but what would you say about below solution?

Replace Lo, M and T as fragments. So after L you would have an activity (PostL) where you show first Lo fragment, then on user action you would add on top of Lo an M fragment by get(Supported)FragmentManager.beginTransation().add(id, fragment).commit(), then moving on you would replace M by T: get(Supported)FragmentManager.beginTransation().replace(id, fragment).commit(). - or you can add T on top of M, it's your choice as it might suite better.

In this PostL activity, in onPause() method you would call only get(Supported)FragmentManager.popBackStack() if the fragment backstack is consisted of more than one fragment - the bottom fragment is Lo.

In this way would be covered in both cases when the user taps on Home screen and re-enters the app (no matter from where), or any other app goes in the foreground. Also, if you consider correctly the backstack size, if the user taps back then he will always open the last fragment (that could be M or Lo).

I hope it makes sense ...

gunar
  • 14,660
  • 7
  • 56
  • 87
  • Well, Thanks for your suggestion first. But this looks like a whole new branch of development changes, to which I have currently most of the finished product. Anyway, Thanks, and if I couldn't resolve this out in any other way I will try this solution too, and surely I will inform you what happened ok?:) thanks once again! – Randika Vishman Jun 17 '13 at 01:47
  • You shouldn't have that work to do as it's relatively easy to migrate an `Activity` to a `Fragment` and vice-versa. Their life-cycle is very similar, callbacks are similar, so you shouldn't have too much work. You could allocate let's say 2 hrs for this, if it works, then Ok, if not, you can leave it as it is. – gunar Jun 17 '13 at 05:59
  • Thanks for the help! So if I'm going with this solution, I would have to migrate all the other Tasks from Activities to Fragment based manner right? If not again, it would cause a problem later. – Randika Vishman Jun 17 '13 at 06:50
  • I'm sorry guys but it's bit silly to say, however I found a half way solution to my question by getting the help of the following [answer](http://stackoverflow.com/a/5862048/651422) Hope this would be helpful to you all in case you met the same question. – Randika Vishman Jul 12 '13 at 04:22
  • Have a look at `Radu`'s comment from that answer. That's a hacky way of doing nothing. – gunar Jul 12 '13 at 06:13
  • That's true when we see it at a glance. But however based on that answer I used a custom method to detect when it's coming from Background, and also that answer which I have given the link, I had only half the problem solved. Where a user clicks on the App Icon and and the app is gained to foreground surprisingly backstack was cleared up. But if the user accessed it within Recent apps, the same problem were still existed, which is the user was able to access the previous Activities. :( I don't even like to remember that headache!!! – Randika Vishman Jul 12 '13 at 08:35