how to call OnResume and Onpause methods in fragment? - android

i have two fragments A and B.here I am Replacing fragment B from A And then fragment A from B in between i am showing toast in onPause method and onResume method but some how its not working could any one explain me why with code ?

The fragments onResume() or onPause() will be called only when the Activities onResume() or onPause() is called. They are tightly coupled to the Activity.
Managing the lifecycle of a fragment is a lot like managing the lifecycle of an activity.
Resumed :->
The fragment is visible in the running activity.
Paused :->
Another activity is in the foreground and has focus, but the activity in which this fragment lives is still visible (the foreground activity is partially transparent or doesn't cover the entire screen).
Stopped :->
The fragment is not visible. Either the host activity has been stopped or the fragment has been removed from the activity but added to the back stack. A stopped fragment is still alive (all state and member information is retained by the system). However, it is no longer visible to the user and will be killed if the activity is killed.
for more information read from android docs

Related

Android fragment lifecycle when bringing fragment back from background

Scenario: fragment is visible, user minimises the app and brings it back from background.
Why onCreateView onCreate is not being called and it jumps straight to calling onStart?
When is onCreate onCreateView called? How to restore fragment data if it jumps straight to onStart?
as stated here:
https://developer.android.com/guide/components/fragments#Lifecycle
"The fragment isn't visible. Either the host activity has been stopped or the fragment has been removed from the activity but added to the back stack. A stopped fragment is still alive (all state and member information is retained by the system). However, it is no longer visible to the user and is killed if the activity is killed."
And you can save the data using the OnSaveInstanceState(Bundle) on the onStart() method

First Activity not stopped when Second Activity started

As per Android activity life cycle, when activity is no longer visible onStop will get called. But this is not happening, if i exit the second activity quickly.
I have two activities HomeActivity and DetailActivity.
Step 1. Pressing a button on HomeActivity navigates to DetailActivity
Step 2. Back press on DetailActivity navigates to HomeActivity
When navigates to DetailActivity , the onStop of HomeActivity should get called as per activity life cycle, since the DetailActivity gets onStart.
But onStop is not getting called if i back press on DetailActivity quickly.
Activity life cycle if back pressed immediately..
HomeActivity: onPause
DetailActivity: onStart
DetailActivity: onResume
DetailActivity: onPostResume
DetailActivity: onPause
HomeActivity: onResume
HomeActivity: onPostResume
DetailActivity: onStop
Activity life cycle if back pressed after a delay (say few seconds later on DetailActivity)
HomeActivity: onPause
DetailActivity: onStart
DetailActivity: onResume
DetailActivity: onPostResume
HomeActivity: onStop
DetailActivity: onPause
HomeActivity: onStart
HomeActivity: onResume
HomeActivity: onPostResume
DetailActivity: onStop
Doesn't this look like a bug as new Activity is Started, the previous activity is not stopped?
According to Android Docs about stopping and restart of an Activity
The user performs an action in your app that starts a new activity. The current activity is stopped when the second activity is created. If the user then presses the Back button, the first activity is restarted.
They should update it to say If the user the presses the Back button but not to quickly haha, but I think iheanyi is right in the fact that some other process may have been granted cpu time after the DetailsActivity hit onPostResume and the back button was pressed before the OS could continue with the onStop process of the HomeAcitvity
You've described normal behavior. Here's what https://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle says about this:
Called when the activity is no longer visible to the user, because
another activity has been resumed and is covering this one. This may
happen either because a new activity is being started, an existing one
is being brought in front of this one, or this one is being destroyed.
Followed by either onRestart() if this activity is coming back to
interact with the user, or onDestroy() if this activity is going away.
So, you may be wondering, well, if my new activity is visible, doesn't that mean the previous activity is invisible? Not really. Your new activity may not completely overlap the old one (in which case, part of the old is still visible). This could be especially tricky if the old activity has elements that while "invisible" to the user because they look the same as elements from the new activity, are technically still visible (in that they're not completely blocked by the new).
Visibility aside, you could also inadvertently have lifecycle methods timing issues which lead to this. Let's say your onPause() takes a long time to complete. The other activity could completely hide the old, satisfying the conditions for onStop(), but you transition back before onStop() has a chance to run.
Finally, there are many tasks that need to run on the phone and few processors to execute them. Necessarily, some tasks will have higher priority than others. Think about the code that must execute when your new activity is taking over the foreground - there is no way that onStop() has time to run if the phone is busy executing code for your activity. If that code then triggers onResume, onStop would be skipped altogether.
As you can see from the life cycle diagram, you can transition from onPause() back to onResume() without going through onStop(). Perhaps you should think about the scenarios where something like that can happen (and why) to better understand your particular case.

Will Activity onpause be called when switching between fragments

Right now I have 2 activities. When the second activity is running, the first activity onpause is clicked which means I have to unregister all the listeners (which is not what I want when the app is running, I just wanted them unregistered when the app is in the background).
So would it make sense for me to create 2 fragments with one activity. Then I can unregister the listener when the activity onpause is called (only when app goes to background) but it won't be called when fragments are switched.
Is my understanding correct?
Thank you
This totally depends on the activity that you have used to launch your fragment . If two fragments A and B belong to the same activity, then the moment you switch view from Fragment A to Fragment B, your activity does not go to a paused state, means onPause() won't be called, but is still running which is ideally providing the view for Fragment B.
So, you do not need to unregister the listener on change of Fragment in the same activity.
Please correct me if I am wrong.
Yups Activity onPause() would not be called when switching fragments.

android -does calling finish from activity destroy hosted fragment?

I have an 'Activity A' which host a 'Fragment B'. Please confirm that if I call A.finish() then 'fragment B 'gets destroyed. The fragment B would already be added to the fragment manager. I realize that a fragment is a sub-activity but i want to know do i have to tell the fragment manager to release the fragment in Ondestroy or is it all taken care of ?
but i want to know do i have to tell the fragment manager to
release the fragment in Ondestroy or is it all taken care of ?
By default when you call finish in your activity it will automatically call onDestroy method of all the fragments that is attach to the activity thus destroying it, so no need to worry about destroying the fragment in onDestroy method of your activity.
From the documentation:
For example, when the activity is paused, so are all fragments in it,
and when the activity is destroyed, so are all fragments.
Fragments are the Subactivities of an activity. So whenever we will call the finish the fragment associated with it will also get destroyed without notifying FragmentManager.
Yes the fragment is destroyed. From the Android Fragment guide:
For example, when the activity is paused, so are all fragments in it,
and when the activity is destroyed, so are all fragments.

Fragment and parent activity life cycle together

I was following these four topics Creating a Fragment, Handling the Fragment Lifecycle , Managing the Activity Lifecycle and Pausing and Resuming an Activity. So I am in a little doubt about this.
My question is
If A Activity call B Activity through Intent but A does not call finish() method then A will be in Pause state if B is Transparent or SemiTransparent and in Stop state if B is Opaque. Am I right?
If A Activity contains Fragment F then if A will go to Pause state then F will go to Pause state and if A will be in Stop state then F will be in Stop state too. Am I right?
If A calls B Activity and B is Transparent then A will be in Pause state and F will too. If B call finish() then A will come to Resume state but what will happen to F? will it come to resume from pause? If it is then how and what steps because I have not seen any direct link in Fragment life cycle which indicates onPause() to onResume() directly as Activity can do.
Hope I am able to ask what I want. Sorry for my bad Englsh.
You can't be sure that only onPause will be called on A if B is SemiTransparent or partially visible as I understand it:
Paused
Another activity is in the foreground and has focus, but this
one is still visible. That is, another activity is visible on top of
this one and that activity is partially transparent or doesn't cover
the entire screen. A paused activity is completely alive (the Activity
object is retained in memory, it maintains all state and member
information, and remains attached to the window manager), but can be
killed by the system in extremely low memory situations.
Yes, you are right:
The lifecycle of the activity in which the fragment lives directly
affects the lifecycle of the fragment, such that each lifecycle
callback for the activity results in a similar callback for each
fragment. For example, when the activity receives onPause(), each
fragment in the activity receives onPause().
However, the opposite is not true, meaning that if a fragment receives onStop, that does not guarantee that the Activity's onStop will be called.
I am not quite sure what you mean by your last sentence or how you have tested this. According to the Fragment documentation:
public void onResume ()
Called when the fragment is visible to the user
and actively running. This is generally tied to Activity.onResume of
the containing Activity's lifecycle
It says generally because it depends on how the fragment is handled by the activity.
If A Activity call B Activity through Intent but A does not call finish() method then A will be in Pause state if B is Transparent or SemiTransparent and in Stop state if B is Opaque. Am I right?
Yes true
If A Activity contains Fragment F then if A will go to Pause state then F will go to Pause state and if A will be in Stop state then F will be in Stop state too. Am I right?
Yes correct
If A calls B Activity and B is Transparent then A will be in Pause state and F will too. If B call finish() then A will come to Resume state but what will happen to F? will it come to resume from pause? If it is then how and what steps because I have not seen any direct link in Fragment life cycle which indicates onPause() to onResume() directly as Activity can do.
What you understood is correct, even in this scenario also fragment will be moved from onPause to onResume state just like an activity.
But unfortunately there is not much documentation about this in developer android.
This might be because they wanted to avoid complicated diagrams which can create more confusion.

Categories

Resources