March 23, 2015

Android Trivia: Fragment Attachment

Imagine that:

You have a FragmentActivity which has a Drawer and the fragment attachment order was:

- Drawer
- Fragment A
- Fragment B.

Note: By order I mean that I'm adding the fragments on a list by the order that each is received on onAttachFragment. So when entering the Activity A, the order was the one above.

Imagine that you go to background, and you Activity A is killed, and when you reopen the app it restores its state. 

Do you know what the attachment order will be?

- Fragment A
- Fragment B
- Drawer

Yes. The Drawer will be the last. So if you are maintaining some of your own stack order using the Fragments lifecycle beware!