Android snippet change header
I have a problem with my fragments. Basically, I have an app that has a navigation drawer and 4 buttons in action. When each button is pressed, a different section will appear on the display. The problem is I want to display the correct title in the ActionBar. I can display the selected item from the navigation to the ActionBar. But when I clicked the button to navigate to another fragment, I cannot display the title in the ActionBar. Any ideas?
Here is my sample code: HomeActivity.class
private void displayView(final int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 0:
fragment = new HomeFragment();
break;
case 1:
fragment = new MyProfileFragment();
break;
case 2:
fragment = new AboutFragment();
break;
default:
break;
}
if (fragment != null) {
fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).commit();
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
setTitle(navMenuTitles[position]);
drawer.closeDrawer(mDrawerList);
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
}
HomeFragment:
public class HomeFragment extends Fragment {
public HomeFragment() {
}
CardView card_cart, card_scan, card_categories, card_about;
Intent i;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
initControls(rootView);
return rootView;
}
private void initControls(View rootView) {
card_cart = (CardView) rootView.findViewById(R.id.card_cart);
card_scan = (CardView) rootView.findViewById(R.id.card_scan);
card_categories = (CardView) rootView.findViewById(R.id.card_categories);
card_about = (CardView) rootView.findViewById(R.id.card_about);
card_categories.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
i = new Intent(getActivity(), CategoriesActivity.class);
startActivity(i);
}
});
card_cart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MyCartFragment nextFrag = new MyCartFragment();
getFragmentManager().beginTransaction()
.replace(R.id.frame_container, nextFrag)
.addToBackStack(null)
.commit();
}
});
card_about.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AboutGrabHutFragment aboutFrag = new AboutGrabHutFragment();
getFragmentManager().beginTransaction()
.setBreadCrumbTitle(getResources().getString(R.string.about))
.replace(R.id.frame_container, aboutFrag)
.addToBackStack(null)
.commit();
}
});
card_scan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
i = new Intent(getActivity(), ScanActivity.class);
startActivity(i);
}
});
}
HomeFragment will find these 4 buttons. Pressing the button, go to another fragment. I can show that with this:
card_about.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AboutGrabHutFragment aboutFrag = new AboutGrabHutFragment();
getFragmentManager().beginTransaction()
.setBreadCrumbTitle(getResources().getString(R.string.about))
.replace(R.id.frame_container, aboutFrag)
.addToBackStack(null)
.commit();
getActivity().setTitle("About");
}
});
But the problem is when I come back, the title is still about. Hope someone knows how to fix this problem. Thank.
In the required snippet of the onCreateView () method, call the setTitle ("") method of the main activity. This way you don't need to check for a second click.
it worked for me
eg ***
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.id.your_fragment_layout, container,
false);
getActivity().setTitle("Your actionbar title");
return rootView;
}
Update:
you can change the title by overriding the method onResume
. And for the safe side, you can also override setUserVisibleHint
and change the title when you fragment isVisibleToUser
.
source to share
I used the onBackStackChangedListener in my MainActivity and used it to update all kinds of things when navigating in the app. You are already manually changing the title, try something like this:
//manager is my FragmentManager
manager.addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
@Override
public void onBackStackChanged() {
//I'm using this check to check if i'm on my MainFragment, you can use this in al kinds of ways. Decide yourself if you use it or not.
if (manager.getBackStackEntryCount() == 1) {
Class<? extends Fragment> FragClass;
//mCurrentFragment is my global Fragment variable to do and check stuff with as below.
FragClass = mCurrentFragment.getClass();
if ((FragClass == DesiredFragment.class)) {
this.setTitle("desired title");
} else if ((FragClass == OtherDesiredFragment.class)) {
//hope this helps.
} //etc..
}
}
});
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
try {
Class<? extends Fragment> FragClass;
FragClass = mCurrentFragment.getClass();
if ((FragClass == HomeFragment.class)) {
System.exit(0);
}
popFragment();
} catch (NullPointerException e) {
e.printStackTrace();
}
}
public void popFragment() {
try {
if (manager.getBackStackEntryCount() > 1) {
manager.popBackStack();
manager.executePendingTransactions();
ArrayList<Fragment> reversedFragments = new ArrayList<Fragment>(manager.getFragments());
Collections.reverse(reversedFragments);
for (Fragment fragment : reversedFragments) {
if (fragment != null) {
try {
mCurrentFragment = (BaseFragment) fragment;
} catch (ClassCastException e) {
e.printStackTrace();
}
break;
}
}
} else {
finish();
}
} catch (NullPointerException e) {
e.printStackTrace();
}
}
source to share