0

I have a main activity which is showing/hiding a bunch of fragments. There is a login activity at the very start that logs the user in. When the user presses home or the multi-tasking button, the app should start a timer that (after the timer runs out) automatically logs the user out. This could be when the user returns to the app (and timer has run out -> logout) or while in the app and has done nothing.

What exactly gets called when the user presses home/multi-tasking button in terms of the fragment's lifestyle methods? In my quick tests, it seems that the first fragment in the fragment manager's onPause is called everytime no matter the fragment on screen when the user exits out.

How can i go about this?

Philipp Jahoda
  • 50,880
  • 24
  • 180
  • 187
John Baum
  • 3,183
  • 11
  • 42
  • 90
  • In your onPause state save, for example, the current time in your apps SharedPreferences. In your onResume method compare the time saved in onPause to the current time and act accordingly. – M.Bennett Aug 28 '13 at 18:58

2 Answers2

1

Please have a look at this tutorial about the Fragment-Lifecycle:

http://developer.android.com/reference/android/app/Fragment.html#Lifecycle

It explains the lifecycle of the Fragment with respect to its Activity.

When the user presses the home-button for example:

onPause() - fragment is no longer interacting with the user either because its activity is being paused or a fragment operation is modifying it in the activity.

Philipp Jahoda
  • 50,880
  • 24
  • 180
  • 187
  • But why is the same fragment's onPause() being called regardless of which is showing on the screen at the time of the home press? – John Baum Aug 28 '13 at 19:04
  • What do you mean by "the same fragment"? – Philipp Jahoda Aug 28 '13 at 19:05
  • I have a fragment manager and it is managing my tranasactions. Each time a user clicks on a button or any area that requires a new screen, the current fragment is hidden and another added (or shown if already in the fragment manager). By same fragment, i mean the same onPause for the very first fragment in the manger is called regardless of if i am on another screen that i transitioned to – John Baum Aug 28 '13 at 19:07
  • @John Baum - All Fragments are bound to an Activity, so all fragments within your Activity will have their onPause() Method called when the Activity holding them is not visible. – M.Bennett Aug 28 '13 at 19:07
  • Oh i think i realized my mistake, i am only logging the onPause for the very first frag. In this case, would it suffice to set a timer on the first fragment's onPause and compare in the onResume? If the time is greater than my timeout, rip out all fragments and display login again? That sound correct? – John Baum Aug 28 '13 at 19:08
  • Why not let the Activity handle this? – M.Bennett Aug 28 '13 at 19:11
0

The methods called on the fragment's lifecycle are the same that the activity's. You can check the complete set of methods that are called right here: Fragments

Oscar Rene
  • 361
  • 2
  • 6