So one of my android Fragments, the detail fragment in a master-detail setup, has several possible layouts, depending on what the user wants to do, and one of those layouts contains about a dozen EditText fields for the user to input data.
If the user inputs data, then switches to another layout or another Fragment (from the Detail to the master Fragment), I want to save what they have input so far before performing the switch, without requiring the user to do anything. So I need to know when a particular layout ID was previously inflated and then gets deflated/destroyed/overwritten/etc.
How do I do this? onCreateView()
, where I am inflating the new layouts, seems insufficient because it doesn't seem to offer a way to access what was there previously, and AFAIK wouldn't be called if the user switches to the master Fragment anyway. getActivity().getCurrentFocus()
at the start of onCreateView()
returns a View, but getID() doesn't seem to match when compared to R.layout.my_layout
, and calling getActivity().getCurrentFocus().findViewByID() != null
on an element in the layout doesn't work either.
Also, when do I perform the saving?
EDIT: Should I override onDestroyView()
and run the save code there? Whenever I try to do so, the app crashes.