I have an Android app with 1 base activity
and a few fragments. They can be changed using the NavigationView
inside the DrawerLayout
. Users can change the language of the application in one of the fragments and when I relaunch the application, I want users to go back to that specific fragment.
=====DrawerLayout=====
1. Fragment Home -> This is the starting fragment
2. Fragment One
3. Fragment Settings -> Users change the language here.
When users change the language, a method in the base activity
is called and I change the Locale, and call recreate(). This will refresh the app with the Fragment Home being displayed in the new language. I want to programatically change to Fragment Settings.
navigationView.<METHOD?>
In order to solve your problem, you can save the special state and recreate the activity. When the activity is recreated, it will know that it needs to move to the settings page using the saved state.
Try this in your activity:
static final String SHOW_SETTINGS = "SHOW_SETTINGS";
private boolean showSettings = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
createLayoutAndDoOtherOnCreateThings();
// Check whether we're recreating a previously destroyed instance
if (savedInstanceState != null) {
// Restore value of members from saved state
if (savedInstanceState.getBoolean(SHOW_SETTINGS, false)) {
showSettingsFragment();
}
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putBoolean(SHOW_SETTINGS, showSettings);
// Always call the superclass so it can save the view hierarchy state
super.onSaveInstanceState(savedInstanceState);
}
private void callRecreateWithSettingsWhenRecreating() {
showSettings = true;
recreate();
}