To fix a change in the name of the box to a change in orientation?

When the orientation changes in my application, the title on the action bar will change from the name of the fragment selected in the navigation box to the title of the application.

The sample application for carrying applications saves the title in a change of orientation, since this fragment is a subclass of the main action and has this line of code

getActivity().setTitle(planet); 

Now I'm a little newbie, so I don’t know how to save the title and implement the code, can you help?

Just in case, any of you want to see, here is a subclass for 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; } } 
+4
source share
4 answers

In the application, the application for the navigation box , here is what I did:

  • In onSaveInstanceState() your activity:

    @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); CharSequence title = mDrawerLayout.isDrawerOpen(mLeftDrawer) ? mDrawerTitle:mTitle; outState.putCharSequence(KEY_STATE_TITLE, title); }

  • In onCreate() your activity:

    if (saveInstanceState!=null) { setTitle(savedInstanceState.getCharSequence(KEY_STATE_TITLE)); }

I hope for this help.

+7
source
Answer to

@maohieng helped me, but he set the wrong title if the box was open when the orientation changed (this was the screen name, not the name of the application, as it should). Therefore, I have implemented this code, which perfectly fixes the problem.

On onSaveInstanceState() I use a boolean to verify that the box is open. It seems odd, but isDrawerOpen(mDrawer) returns false after changing the orientation when I call it onCreate ():

 @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putCharSequence("title", mTitle); outState.putBoolean("isDrawerOpen", mDrawerLayout.isDrawerOpen(mDrawerLinear)); } 

In onCreate() activity:

 if (savedInstanceState == null) { selectItem(YOUR_FRAGMENT); } else { if (savedInstanceState.getBoolean("isDrawerOpen")) { mTitle = savedInstanceState.getCharSequence("title"); } else { setTitle(savedInstanceState.getCharSequence("title")); } } 
+4
source

Found a way to save fragments in the action bar when changing orientation, I'm not sure what the best way is, but it 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 a corresponding array position number. In addition, I took out the unnecessary

 setTitle(titleList[position]); 

from the selection of any element of the navigation box, since now it is processed from the corresponding fragment classes

+1
source

you need 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(); 

I hope for this help.

0
source

Source: https://habr.com/ru/post/1492653/


All Articles