My program has 6 fragments: Fragment1, Fragment2,....->Fragment6.
I use the add() and replace() statement to switch between the fragment and track their lifecycle.
Fragment1 add Fragment2 add Fragment3 add Fragment4 add Fragment5 replace Fragment6
The log-cat to shown their lifecycle (i have some printf-points in onCreate, onCreateView, onDestroyView, onDestroy for tracking)
Tag ______________ Text
Fragment1_________onCreate
Fragment1_________onCreateView
Fragment1_________add Fragment2
Fragment2_________onCreate
Fragment2_________onCreateView
Fragment2_________add Fragment3
Fragment3_________onCreate
Fragment3_________onCreateView
Fragment3_________add Fragment4
Fragment4_________onCreate
Fragment4_________onCreateView
Fragment4_________add Fragment5
Fragment5_________onCreate
Fragment5_________onCreateView
Fragment5 _______ replace Fragment6
Fragment1 _______ onDestroyView
Fragment3 _______ onDestroyView
Fragment5 _______ onDestroyView
Fragment6_________onCreate
Fragment6_________onCreateView
My questions:
Why after the Fragment5 is replaced by Fragment6, the Fragment1 & 3 &5 are destroyed their view ?.
What is happending with Fragment2 & 4 ?
Why Fragment2 & 4 are not destroyed their view as Fragment1 & 3 &5 ?
Please help me to understand fully about fragment's lifecycle when call the add() and replace() method.
Update my addFragment and replaceFragment method:
public void addFragment(Fragment fromFragment, Fragment toFragment) {
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.container,toFragment, toFragment.getClass().getName());
transaction.hide(fromFragment);
transaction.addToBackStack(toFragment.getClass().getName());
transaction.commit();
}
public void replaceFragment(Fragment fromFragment, Fragment toFragment) {
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.container,toFragment, toFragment.getClass().getName());
transaction.hide(fromFragment);
transaction.addToBackStack(toFragment.getClass().getName());
transaction.commit();
}
ADHD is the official, medical term for the condition — regardless of whether a patient demonstrates symptoms of hyperactivity. ADD is a now-outdated term that is typically used to describe inattentive-type ADHD, which has symptoms including disorganization, lack of focus, and forgetfulness.
ADD, or attention-deficit disorder, is an old term, now out of date, for the disorder we call ADHD, ADHD. see attention-deficit hyperactivity disorder. or attention-deficit hyperactivity disorder.
Attention-deficit disorder (ADD) and attention-deficit/hyperactivity disorder (ADHD) are indeed the same condition, it's just that ADHD has had several name changes in the last three decades. 1 This is because as more research is carried out, understanding grows and the name has been changed to reflect that knowledge.
ADD is the term commonly used to describe symptoms of inattention, distractibility, and poor working memory. ADHD is the term used to describe additional symptoms of hyperactivity and impulsivity. Both are included in the medical diagnosis of attention deficit hyperactivity disorder.
If you use a FragmentTransaction
to hide the fragment, then it can still be in the running state of its lifecycle, but its UI has been detached from the window so it's no longer visible. So you could technically still interact with the fragment and reattach its UI later you need to. If you replace the fragment, then you are actually pulling it out of the container and it will go through all of the teardown events in the lifecycle (onPause
, onStop
, etc) and if for some reason you need that fragment again you would have to insert it back into the container and let it run through all of its initialization again.
When you replace, you're swapping out all of the fragments (1,2,3,4,5) in the ViewGroup
R.id.container
for your new Fragment
(6). Once the fragments have been removed, they will be destroyed. When destroyed, they will call the onDestroyView()
method.
FragmentTransaction replace
With regards to your question for 2 and 4, I'm not sure. Are you able to post more of the code what writes out to logcat?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With