I have a problem with my fragments. Basically, I have an app which has a navigation drawer and 4 buttons on the activity. When each button is pressed, it will display another fragment. The problem I have is that I want to display the correct title on the ActionBar
. I can display the selected item from the navigation on the ActionBar
. But when I pressed the button to go to another fragment, I cannot display the title on the ActionBar
. Any ideas?
Here's 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);
}
});
}
In the HomeFragment
will those 4 buttons be found. On a button click, will go to another fragment. I can display that using 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 go back, the title is still About
.