1

I have created a personal contact book, with around 10 activity as per the business requirements for groups, contacts, editing, viewing, deleting-multiple, etc.

If application is moved to background by home-button or another application, I need to catch the event when application returns from background to show the login screen, everytime.

Also, please note if another activity of the same contact-book application, login screen should not be shown.

I tried using onResume, onStart, onPause and onStop events of each activity by overriding the functionality in various activites for login, but this doesn't satisfies as if the application is in foreground and just an activity is changed, then also these are also called.

Please suggest, thanks !!!

HarsH
  • 760
  • 3
  • 12
  • 27

5 Answers5

1

Probably the best place for such functionality will be the onCreate method. It's called just at the time when the application is created from scratch. The opposing method is onDestroy which is called only when the system kills your application for the sake of freeing some memory (which happens rather rarely), or your application is explicitly killed by some kind of Task Killer. This means that when your activity goes to background and then back to foreground - the onCreate won't be called, and you should assume this. Hope this helps.

Egor
  • 39,695
  • 10
  • 113
  • 130
  • 1
    I guess u have missed what i am trying to say, I want to ask for login each time the application comes to foreground. – HarsH Dec 27 '11 at 13:46
0

I think you can put a Boolean variable to true when onPause() and onStop() and check on this variable in onResume() to show the login dialog or not. If you need more persistant way, you can save it in shared preference in onPause() and onStop and remove it from the shared preference in onDestroy(). But note that Android Garbage collector will remove your App from memory if another App in focus needs the memory.

Mohamed_AbdAllah
  • 5,311
  • 3
  • 28
  • 47
  • As I use 10 activities in my application, and i move from one activity to another, each time onPause and onResume are called, where login should not be asked, right ? Login should be asked everytime when app comes to foreground. – HarsH Dec 27 '11 at 13:49
  • 1
    You could pass a variable from Activity to another (in the Intent) and remove it in the onResume of the called Activity. If the variable is not included in the Intent, this means that the call is not from another Activity, so you can then show the login. – Mohamed_AbdAllah Dec 27 '11 at 14:50
0

Try overriding onRestart() of the activity class.

Description for onRestart().

I hope it helps..

R.daneel.olivaw
  • 2,681
  • 21
  • 31
  • onRestart is called everytime the activity comes to foreground, though the application is running, i only need check whether the app comes from background or not. – HarsH Dec 27 '11 at 13:54
  • The documentation says..'when the current activity is being re-displayed to the user (the user has navigated back to it)'. It means that it will not be called when the activity starts the first time. Isn,t this what you want ? – R.daneel.olivaw Dec 27 '11 at 14:18
0

I do something similar, and my solution is admittedly hacky but it works for me.

All of my Activities always derive from a common base class in all of my applications. I call it ActivityBase, for reference (and it extends Activity of course). In ActivityBase.onPause I save the current time millis and you can optionally save the name of the derived class to know exactly which Activity was paused last. If you need to run code when the app has been in the background and returns to the foreground, you can check the difference of the current time to the time that the last Activity in your app was paused. If the app is still in the foreground, the difference should be no greater than the max amount of time that it takes to pause one Activity and resume the next. If it's greater than a second or two (and this is the hacky part, you're picking an arbitrary time measurement that means "too long to be an in-app navigation"), then you can call whatever code should run when your app is returning to the foreground.

Rich
  • 36,270
  • 31
  • 115
  • 154
0

Finally, I found it myself.

I managed writing a base class over all activities where setting and getting variable to and from Intent is done respectively. So if application moves to background and comes to foreground later, the intent variable is not found, where i shown the login screen, rather entering to the application.

Thanks, Everyone who suggested approaches, let me know if gone in wrong way or wrong approach anywhere. Thanks to @midoalageb too..!

Thanks

HarsH
  • 760
  • 3
  • 12
  • 27