I'm currently working on a project that has an activity which is consisted of two fragments.
- The first fragment shows a custom expandable list. Every row is created from a custom layout that has a checkbox in the right side of it.
- The second fragment shows more details about the clicked row from the list. In order to open the second fragment, the user has to click on the row. The checkbox is used for another reason.
So, what I'm trying to do is to display these two fragments side by side only when the application runs in tablets. When the app runs in handsets and the user presses one row, the second fragment should be displayed on top.
Furthermore, I have an action bar at the top of the screen which has implemented the usual back button.
The problem exists when I open the second fragment when I have already selected some checkboxes. When I press the back button, which navigates me to the first fragment, the checkboxes will not be checked.
The onSaveInstanceSate method is obviously not called (as the parent activity is not getting paused), so I can't save the ArrayList that stores the checked rows.
Last but not least, the fragments are being added dynamically.
The question
How can I properly implement the back button so when the user uses a
- tablet, the back button should be used in order to close the activity, or a
- handset, so the back button should be used as a navigation back to the first fragment with the ability to restore it's previous state?