2

I'm trying to develop an application in which a login class is called whenever user switches to some other application or presses Home button.

For example, I have two activities Activity1 and Activity2.

  • Activity2 is called from Activity1, so that mean Activity1 goes to onPause state.
  • When user switches to some other application, Login class should be called from that activity and on successful authentication it resumes at the same point.

And Currently I'm starting an intent for Login class in every Activity's onPause method. So when I switch from Activity1 to Activity2, onPause method of Activity1 is getting executed and I'm not able to switch to Activity2.

To put it simple, Login class should be called only when user switches to some other application or presses Home button but not when the application is in foreground.

Hope you understand my problem. Thanks !

Srujan Simha
  • 3,637
  • 8
  • 42
  • 59
  • You want the user to have to authenticate each time he comes back to the app? Is that the question? – Rarw Feb 06 '13 at 17:03
  • @Rarw Ya exactly ! And resumes at the place where he was before. – Srujan Simha Feb 06 '13 at 17:06
  • Is it that you want to maintain some sort of state across different Activities, such that you don't need to authenticate in each of them, but only once, and after that switching activities should recognize the user as authenticated? – Nazar Merza Feb 06 '13 at 17:23

4 Answers4

0

Override Application and set/reset the login Preference in Application onCreate()/onTerminate().

Hope this helps.

Piyush
  • 18,895
  • 5
  • 32
  • 63
Gaurav Arora
  • 1,805
  • 13
  • 13
0

Use a boolean flag.

boolean movingInApp = false;
....
movingInApp = true;
Intent intent...
.....
public void onPause() {
    if(!movingInApp) {
        //Show login
    }
}

public void onResume() {
    movingInApp = false;

}

By setting the value of movingInApp to true before launching any intent etc, you can prevent your app from showing the login screen. Remember to set it to false again later in your onResume() method. If the system makes your app go to the background, this will be false, and your login screen will be shown.

An even better method of doing this would be to implement this logic in some BaseActivity, and have all your other Activities inherit from it.

Raghav Sood
  • 81,899
  • 22
  • 187
  • 195
  • Thanks ! Its working. I already tried this but forgot to change the value in onResume. You have solved one issue, another one is its not resuming in the same activity. Any idea? – Srujan Simha Feb 06 '13 at 17:53
  • @SrujanSimha Do you mean your app doesn't come back to the same Activity when resumed? – Raghav Sood Feb 06 '13 at 17:54
  • Ya exactly. Do I have to do something more to achieve this? – Srujan Simha Feb 06 '13 at 17:56
  • @SrujanSimha There are many many reasons for that happening. I can't say just by using comments to find out. It depends heavily on your Activity lifecycle and application lifecycle and what order and how your activities are launched in. You should open a separate question for that with all these details. – Raghav Sood Feb 06 '13 at 17:57
  • 1
    Ok sure. Thanks once again ! – Srujan Simha Feb 06 '13 at 18:02
  • man, wouldn't it be much better if the `Application` class would just have `onPause()` and `onResume()` methods. This would prevent all these nasty workarounds to achieve something so simple – slinden77 May 27 '13 at 20:45
  • @dmmh Perhaps, but how would you tell if an entire app has been paused or resumed? :P – Raghav Sood May 27 '13 at 20:48
  • uhm, when it's no longer visible it's paused and when it's resumed it's resumed? :p Just like for the `Activity` class? :p Naturally the `Application` class should then provide these methods, which actually was what I commented ;) Then you simply subclass it and throw all methods you need in the `Application` subclass, instead of per `Activity` :) – slinden77 May 27 '13 at 20:55
  • @dmmh But the Application class is also accessed by Services and Receivers. They can't be "paused" and "resumed" in a well define way like Activities :P – Raghav Sood May 27 '13 at 20:59
  • there's a solution to every problem, I am just pointing out one of the many obvious missing platfrom components :p Besides, be that as it may, those components could still be kept running, even if `onPause()` and `onResume()` were to be implemented. Just a matter of proper documentation. Plus, services and receivers can't be paused anyway :p – slinden77 May 27 '13 at 21:00
0

You will have to implement something like this on your own. Android's concept of an Application differs from other platforms; users do not interact with the Application, but rather with individual application components (Activities). You could implement this in an Application subclass, but the system can keep the Application around even after the user leaves your activities (by pressing HOME, for instance).

One solution I did for such a problem is to make a singleton class (perhaps called LoginManager) with lifecycle callbacks that mirror the Activity lifecycle callbacks. Then make a BaseActivity class which obtains the singleton and where each lifecycle callback calls the corresponding method in the singleton. This way you can track when your activities through their lifecycle and react accordingly.

I watched for activities to be started, resumed, paused, or stopped. When opening an activity, the current one pauses, the next on is created, started, and resumed (or restarted and resumed), and then the first is stopped.

Karakuri
  • 38,365
  • 12
  • 84
  • 104
0

I'm not 100% clear on how you're implementing this now but based on the description it sounds like you would want to implment this logIn class onResume() rather than in onPause(). If you're trying to have the user authenticate each time an activity comes back to the foreground then I would do the following:

(1) store the last state of the app to SharedPreferences onPause() and/or onStop()

(2) onResume() or onRestart() use an intent to direct the user back to the logIn activity (or authenticate automaticlly depending on how you are doing this)

(3) if authentication is successful, access the last state stored in SharedPreferences and return back to the actity the user was in before

Rarw
  • 7,645
  • 3
  • 28
  • 46