简体   繁体   中英

Android: Change activity title when navigating fragments; Bring back same titles when back button clicked

I Have a fragment called categories_collection the application initially launches with this fragment and the activity title is the application title.

Category 1
Category 2
- Category 2.1
- Category 2.2
Category 3

what i do is when a user clicks on a category that has children i re-call the same categories_collection fragment with an extra field called parent to load the categories that are the children of the clicked category. this is all done and working well.

What i need is the following

  1. User clicks on category 2. the Main activity title changes to "Category 2"
  2. The user is now browsing the children of "Category 2" when clicking on a child. the Activity title changes to the sub category name which is "Category 2.1" and so on..
  3. Most importantly when clicking the "Back Button" the title of the Activity changes to what it was. so lets say i went back from opening the sub category so the title changes to "Category 2" again as i keep browsing the children of "Category 2"

I hope this explains what i need.

Thanks.

Make a method and call it on backpressed :

void onBack() {
//Find all your fragments with id
        Categroy_Child_Fragment fr = (Categroy_Child_Fragment) fragmentManager
                .findFragmentByTag("list_items");

        Final_Categories_Fragment fr1 = (Final_Categories_Fragment) fragmentManager
                .findFragmentByTag("last");
        Grid_View_Fragment_For_All fr2 = (Grid_View_Fragment_For_All) fragmentManager
                .findFragmentByTag("grid_view");


//Then check if they are null or not and according to that remove them and settitle to actionbar

        if (fr2 != null) {
            fragmentManager
                    .beginTransaction()
                    .setCustomAnimations(R.anim.out_to_left, R.anim.out_to_left)
                    .remove(fr2).commit();

            actionBar.setTitle("title");
        } else if (fr1 != null) {
            fragmentManager
                    .beginTransaction()
                    .setCustomAnimations(R.anim.bottom_down, R.anim.bottom_down)
                    .remove(fr1).commit();

            actionBar.setTitle("title");

        } else if (fr != null) {
            fragmentManager.beginTransaction()
                    .setCustomAnimations(R.anim.right_out, R.anim.right_out)
                    .remove(fr).commit();

            actionBar.setTitle(R.string.all_cat);

        } else {

            super.onBackPressed();
        }
    }

In every fragment that should change its activities' title I put something like this in the onResume() method of the fragment.

@Override
public void onResume() {
    super.onResume();
    ((ParentActivity) getActivity()).setTitle("FragmentTitle"); 
}

and then you have to create setTitle(String title) method in your activity that should change its title.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM