AppCompat Slice Life Cycle Changed

After upgrading to the new appcoppat library com.android.support:appcompat-v7:25.1.0 I have a new fragment life cycle when replacing fragments in a transaction.

eg. I have two fragments FrFirst and FrSecond with logs in onStart and onStop , and first I replace the second, and then the second: FrFirst -> FrSecond -> FrFirst .

 getActivity().getSupportFragmentManager() .beginTransaction() .replace(R.id.content, new FrSecond()) .commit(); 

In a previous version of appcompat, I can read these logs:

FrFirst: go to the second
Frst: stop
FrSecond: start

FrSecond: go to first
Frsecond: stop
FrFirst: The Beginning

At 25.1.0 these logs:

FrFirst: go to the second
Frsecond: start
Frst: stop

FrSecond: go to first
FrFirst: get started
Frsecond: stop

So, now the onStart view of the fragment called before this onStop current.

Why is the order of the methods changed, is it a bug in the support library?

+5
source share
1 answer

This is the intended behavior of the new appcompat. As described here https://code.google.com/p/android/issues/detail?id=230415 this

new features to optimize operations and transfer delayed fragments, which is a side effect.

You can disable fragment optimization by calling FragmentTransition.setAllowOptimization (false). This makes everything happen in the correct order, but also prohibits the optimization of operations.

So, if you want to see the old behavior, you can replace fragments with optimization disabled:

 getActivity().getSupportFragmentManager() .beginTransaction() .replace(R.id.content, new FrSecond()) .setAllowOptimization(false) .commit(); 
+6
source

Source: https://habr.com/ru/post/1261548/


All Articles