Created
August 19, 2015 16:12
-
-
Save jpetitto/62b487cd046ee80ef28d to your computer and use it in GitHub Desktop.
Save Fragment state across config changes
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
public class MainActivity extends AppCompatActivity { | |
private Fragment savedFragment; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
if (savedInstanceState != null) { | |
savedFragment = getSupportFragmentManager().getFragment(savedInstanceState, "content"); | |
} else { | |
savedFragment = new UserInfoFragment(); | |
getSupportFragmentManager() | |
.beginTransaction() | |
.replace(R.id.content_frame, savedFragment) | |
.commit(); | |
} | |
} | |
@Override | |
protected void onSaveInstanceState(Bundle outState) { | |
super.onSaveInstanceState(outState); | |
getSupportFragmentManager().putFragment(outState, "content", savedFragment); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment