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.
Related
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
I trace lifecycle method's callback from Activity and Fragment components and i have a lot of questions. I need you help to understand some points.
Situation 1:
One launcher Activity with one Fragment, which filled it. When app started, Fragment become active and user see only Fragment.
Lyfecycle:
Activity: onCreate
Activity: onStart
Fragment: onAttach
Fragment: onCreate
Fragment: onCreateView
Fragment: onViewCreated
Fragment: onActivityCreated
Activity: onResume
Fragment: onResume.
Questions:
1) There is no onResume between 2 - 3, because of Activity lose focus?
2) I don't think i understand what happen on 8 - 9. It seems like some kind of "switch focus" between Fragment and Activity. Ok, i can understand: 9 is about Fragment came to the foreground, take focus and onResume was called. But what is 8? When i load Fragment(since 3), Activity is never on foreground.
Situation 2:
Like situation 1, but now Fragment and Activity loaded and i pressed Home:
Activity: onPause
Fragment: onPause
Activity: onStop
Fragment: onStop
Questions:
When i run it, i expect another behavior. I thought Fragment should be the first who called stop methods, like (Fragment onPause -> Fragment onStop - > Activity methods). Why it worked this way?
Situation 3:
Like situation 2, Activity with Fragment are back to the foreground. I remember, they both have Stopped status (because of onStop was called).
Activity: onStart
Activity: onResume
Fragment: onResume
Questions:
I expect 1-2, but i expect that Fragment call onStart, because, if i understand it correct, onStart called when fragment become visible. Why onStart was't called?
Fragment and Activity lifecycles work in parallel. The linear dependence between their lifecycle ends as soon as both activity and fragment are created.
The following figure explains how the two lifecycles are interconnected.
Note that onStart and onResume (and similarly, onStop and onPause) for both fragment and activity execute in parallel and there is no guarantee of order. Sometimes fragment will take precedence over activity, and vice versa.
The only guarantee is that activity's onCreate will always be called first. After that, fragment acts on its own.
I am facing a strange issue
Activity A gets called and Fragment A has been attached to it.
Method gets called (onCreate, onStart,onResume) .
: I received the notification and I tab on that with intent for Activity A.
Blockquote
(intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
)
When Activity A is opened it call the below method.
(onPause,onCreate, onStart,onResume (Fragment detached) and than onDestroy)
Why on destroy is called for the old instance of the activity when I have already called CLEAR_TOP.
Can someone help me what can be the cause for this?
How can I check if there is any instance is pending in stack already or how can I clear everything?
I can not use singleInstance as on notification tab I am redirecting to different fragment.
If you use only FLAG_ACTIVITY_CLEAR_TOP then all instances of activities will be cleared, back to and including the instance of the target Activity, and then a new instance of the target Activity will be created.
If you want to reuse an existing instance, you need to specify both FLAG_ACTIVITY_CLEAR_TOP and FLAG_ACTIVITY_SINGLE_TOP like this:
intent.addFlags(FLAG_ACTIVITY_CLEAR_TOP|FLAG_ACTIVITY_SINGLE_TOP);
This will clear all instances of activities back to (but excluding the target Activity, and call onNewIntent() on the target Activity with the new Intent.
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
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.