Created
February 12, 2014 01:42
-
-
Save kristinpeterson/8948386 to your computer and use it in GitHub Desktop.
Android Fragments: Maintaining State After Configuration Change
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// | |
// Changes made to in FeedFragment.java | |
// | |
// | |
// | |
// Added private member to store position | |
// | |
private int position; | |
// | |
// Added setRetainInstance(true); to onCreate() | |
// | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
Log.i(TAG, "Entered FeedFragment.onCreate()"); | |
setPosition(-1); | |
// retain this fragment | |
setRetainInstance(true); | |
} | |
// | |
// Added getter & setter for position member | |
// | |
public int getPosition() | |
{ | |
return position; | |
} | |
public void setPosition(int position) | |
{ | |
this.position = position; | |
} | |
// | |
// Call setPosition() when updateFeedDisplay() is called | |
// | |
void updateFeedDisplay(int position) { | |
Log.i(TAG, "Entered FeedFragment.updateFeedDisplay()"); | |
mTextView = (TextView) getView().findViewById(R.id.feed_view); | |
mTextView.setText(feedFragmentData.getFeed(position)); | |
setPosition(position); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// | |
// Changes made to in FriendsFragment.java | |
// | |
// | |
// | |
// Add setRetainInstance(true); to onCreate() method in FriendsFragment.java | |
// | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
Log.i(TAG, "Entered FriendsFragment.onCreate()"); | |
// use different layout definition, depending on whether device is pre- | |
// or post-honeycomb | |
int layout = Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ? android.R.layout.simple_list_item_activated_1 | |
: android.R.layout.simple_list_item_1; | |
// Set the list adapter for this ListFragment | |
setListAdapter(new ArrayAdapter<String>(getActivity(), layout, FRIENDS)); | |
// retain this fragment | |
setRetainInstance(true); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// | |
// Changes made in MainActivity.java | |
// | |
// | |
// | |
// Check to see if mFeedFragment or mFriendsFragment already exist | |
// in the FragmentManager before instantiating new FriendsFragment | |
// (to avoid redundant instantiation) | |
// | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.main_activity); | |
Log.i(TAG, "Entered MainActivity.onCreate()"); | |
// If the layout is single-pane, create the FriendsFragment | |
// and add it to the Activity | |
if (!isInTwoPaneMode()) { | |
// find the retained friends fragment on activity restarts | |
FragmentManager fm = getFragmentManager(); | |
if(fm.getBackStackEntryCount() == 0) | |
mFriendsFragment = (FriendsFragment) fm.findFragmentById(R.id.fragment_container); | |
else | |
mFeedFragment = (FeedFragment) fm.findFragmentById(R.id.fragment_container); | |
if(mFriendsFragment == null && mFeedFragment == null) | |
{ | |
mFriendsFragment = new FriendsFragment(); | |
//TODO 1 - add the FriendsFragment to the fragment_container | |
FragmentTransaction ft = getFragmentManager().beginTransaction(); | |
ft.add(R.id.fragment_container, mFriendsFragment); | |
ft.commit(); | |
} | |
} else { | |
// Otherwise, save a reference to the FeedFragment for later use | |
mFeedFragment = (FeedFragment) getFragmentManager() | |
.findFragmentById(R.id.feed_frag); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment