Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Android Fragments: Maintaining State After Configuration Change
//
//
// 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);
}
//
//
// 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);
}
//
//
// 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
You can’t perform that action at this time.