简体   繁体   中英

What will be fragment behavior when came from background. Considering fragment is in stack

I have a Navigation panel activity. With 5 fragments (Will name it as Fragment1, Fragment2, ...) in menu sections.

Now by Default, activity will display Fragment1.

If user navigate to Fragmentxtz from Fragment1. We will add the fragment on top of Fragment1.

Now user goes to background by pressing home button and open the app from tasks.

Now i know Fragmentxtz onStart will be called. But i see that Fragment1 onStart is also called.

Is this expected behavior?

As you can see on Android Developers, your fragment will be called at onViewCreated().

https://developer.android.com/guide/components/fragments#Creating

The View has to be updated in case you changed the system language or something like that.

Providing a bit more context to what might be happening here.

If both of your fragments are added eg via FragmentTransaction.add() both of them will have onCreateView() called when the layout is restored for the user as Robert pointed out. From the system's point of view all of those fragments are relevant to the user and would be shown simultaneously.

If on the other hand you add fragments via FragmentTransaction.replace() only the topmost fragment on the back stack will receive the onCreateView() call. This can also be achieved by doing an add and remove of the old fragment. If you make this transaction reversible by the back stack after pressing the back button your previous fragment would receive the appropriate lifecycle callbacks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM