Reputation: 143
When the orientation is changed on my app, the title in the action bar changes from the title of the fragment selected in the nav drawer to the title of the app.
The Navigation drawer example app retains it's title on orientation change because it's fragment is a subclass of the main activity, and has this line of code
getActivity().setTitle(planet);
Now i'm bit a newbie so i wouldn't know how to retain the title, and implement the code, can you guys help?
Just in case any of you wanna see, here is the subclass for the PlanetFragment
public static class PlanetFragment extends Fragment {
public static final String ARG_PLANET_NUMBER = "planet_number";
public PlanetFragment() {
// Empty constructor required for fragment subclasses
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_planet, container, false);
int i = getArguments().getInt(ARG_PLANET_NUMBER);
String planet = getResources().getStringArray(R.array.planets_array)[i];
int imageId = getResources().getIdentifier(planet.toLowerCase(Locale.getDefault()),
"drawable", getActivity().getPackageName());
((ImageView) rootView.findViewById(R.id.image)).setImageResource(imageId);
getActivity().setTitle(planet);
return rootView;
}
}
Upvotes: 4
Views: 5213
Reputation: 21
you have to add setTitle(menuItem.getTitle()); as shown below,
@Override public boolean onNavigationItemSelected(MenuItem menuItem) {
//Checking if the item is in checked state or not, if not make it in checked state
if (menuItem.isChecked()) menuItem.setChecked(false);
else menuItem.setChecked(true);
**setTitle(menuItem.getTitle());**
//Closing drawer on item click
drawerLayout.closeDrawers();
Hope this help.
Upvotes: 0
Reputation: 51
@maohieng's answer helped me but it was setting the wrong title if drawer is open on orientation change (it was the screen name, not the app's title as it should). So I implemented this code which fix the problem perfectly.
On onSaveInstanceState()
I use boolean to check wether drawer is open. Seems odd, but isDrawerOpen(mDrawer)
return false after orientation change when I call it on onCreate():
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putCharSequence("title", mTitle);
outState.putBoolean("isDrawerOpen",
mDrawerLayout.isDrawerOpen(mDrawerLinear));
}
On onCreate()
of Activity:
if (savedInstanceState == null) {
selectItem(YOUR_FRAGMENT);
} else {
if (savedInstanceState.getBoolean("isDrawerOpen")) {
mTitle = savedInstanceState.getCharSequence("title");
} else {
setTitle(savedInstanceState.getCharSequence("title"));
}
}
Upvotes: 4
Reputation: 1736
From the Navigation drawer example app, here what I did :
On onSaveInstanceState()
of your Activity :
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
CharSequence title = mDrawerLayout.isDrawerOpen(mLeftDrawer) ? mDrawerTitle:mTitle;
outState.putCharSequence(KEY_STATE_TITLE, title);
}
On onCreate()
of your Activity:
if (saveInstanceState!=null) {
setTitle(savedInstanceState.getCharSequence(KEY_STATE_TITLE));
}
Hope this help.
Upvotes: 7
Reputation: 143
Found a way to retain frag titles in action bar on orientation change, not sure if the best way, but seems pretty clean to me.
public class Example extends Fragment{
private String[] titleList;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
titleList = getResources().getStringArray(R.array.sell_array);
getActivity().setTitle(titleList[0]);
return inflater.inflate(R.layout.fish, container, false);
}
}
Each fragment has the respective array position number. Also, from Main Activity class, I took out unnecessary
setTitle(titleList[position]);
from on selection of any Navigation drawer item since now that is handled from the respective fragment classes
Upvotes: 1