Android Programming The Big Nerd Ranch Guide by Bill Phillips, Chris Stewart, Kristin Marsicano (z-lib.org)

(gtxtreme123) #1

Chapter 13  The Toolbar


Now that the subtitle always displays the correct number of crimes, solve the rotation issue. To fix this
problem, save the mSubtitleVisible instance variable across rotation with the saved instance state
mechanism.


Listing 13.19  Saving subtitle visibility (CrimeListFragment.java)


public class CrimeListFragment extends Fragment {


private static final String SAVED_SUBTITLE_VISIBLE = "subtitle";
...
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
...
if (savedInstanceState != null) {
mSubtitleVisible = savedInstanceState.getBoolean(SAVED_SUBTITLE_VISIBLE);
}


updateUI();


return view;
}


@Override
public void onResume() {
...
}


@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean(SAVED_SUBTITLE_VISIBLE, mSubtitleVisible);
}
}


Run CriminalIntent. Show the subtitle and then rotate. The subtitle should appear as expected in the re-
created view (Figure 13.13).

Free download pdf