ActionBar name dynamically changed using fragment

I have 1 action with 3 fragments inside (Home-Login-RestorePass). HomeFragment is displayed first, and the other two are hidden. I want the name of the ActionBar to change depending on which fragment is displayed.

I try in my activity with:

public void setActionBarTitle(String title){ getSupportActionBar().setTitle(title); } @Override public void onResume() { super.onResume(); // Set title setActionBarTitle(getString(R.string.app_name)); } 

and fragments have the same:

 @Override public void onResume() { super.onResume(); // Set title ((LoginActivity) getActivity()).setActionBarTitle(getString(R.string.fragment_login)); } 

But that will not work. It always shows R.string.fragment_login in the header.

I use FragmentTransaction to transition a fragment:

 btnLogin.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { FragmentTransaction ft = getFragmentManager().beginTransaction(); ft.setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right); HomeFragment homeFragment = (HomeFragment) getFragmentManager().findFragmentById(R.id.fragmentHome); LoginFragment loginFragment = (LoginFragment) getFragmentManager().findFragmentById(R.id.fragmentLogin); ft.hide(homeFragment).addToBackStack(null); ft.show(loginFragment).addToBackStack(null).commit(); } }); 

Also, if I could appear, the button with the (Back) arrow on the ActionBar would be large depending on the fragment.

Thank you for your time! Best wishes.

+7
android android-actionbar android-fragments
source share
3 answers

Use this method in action to modify the Fragment and programmatically adjust the header:

 private void displayFragment(int position) { // update the main content by replacing fragments Fragment fragment = null; String title = ""; switch (position) { case 0: fragment = new Home(); title = "Home"; break; case 1: fragment = new Login(); title = "Login"; break; case 2: fragment = new RestorePass(); title = "Restore Password"; break; default: break; } // update selected fragment and title if (fragment != null) { getSupportFragmentManager().beginTransaction() .replace(R.id.frame_container, fragment).commit(); getSupportActionBar().setTitle(title); // change icon to arrow drawable getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_arrow); } } 

For example, you want to display a fragment of Home :

 displayFragment(0); 
+9
source share

Keep in mind that if you use the support library, you need to specifically indicate your activity when you receive it through getActivity() . And then you want to make sure that you get ActionBar support with getSupportActionBar() . I was able to set the ActionBar header in my application using the following code in my onResume() ...

 @Override public void onResume() { super.onResume(); AppCompatActivity activity = (AppCompatActivity) getActivity(); ActionBar actionBar = activity.getSupportActionBar(); actionBar.setTitle(R.string.my_fragment_title); } 
+7
source share

If you set the activity attribute android:label="" in AndroidManifest , you can set the initialization after the header. I discovered this trace through the toolbar source code.

0
source share

All Articles