Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
248 views
in Technique[技术] by (71.8m points)

android - Difference between add() & replace() with Fragment's lifecycle

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();
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...