简体   繁体   中英

Android setting fragment toolbar title

I was having some minor bug with fragment transaction. The problem occurs when I opened up more than one fragment and press the back button and the title shown on toolbar is wrong.

For instance, from my Home -> Expenses fragment , and I press back button from Expenses fragment, the title shown on toolbar is correct.

However, let's say Home -> Expenses -> Incomes fragment , and I press back button from Incomes fragment, it will go back to Home fragment, but the title shown on toolbar will be 'Expenses' instead of 'Home'.

Here is my set up for MainActivity which extends AppCompatActivity:

 // inside onCreate() put all these code
//Setting Navigation View Item Selected Listener to handle the item click of the navigation menu
    navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {

        // This method will trigger on item Click of navigation menu
        @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);

            //Closing drawer on item click
            drawerLayout.closeDrawers();

            //Check to see which item was being clicked and perform appropriate action
            final Bundle bundle = new Bundle();
            switch (menuItem.getItemId()){

                case R.id.home:
                    getSupportActionBar().setTitle("Home");
                    return true;

                case R.id.expenses:
                    return true;

                case R.id.incomes:
                    return true;

                case R.id.history:
                    return true;

                case R.id.setting:
                    return true;
            }
        }
    });

Then inside each of my fragments which extends Fragment, I set the title and override the back button:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.activity_expense,container,false);

    ((AppCompatActivity)getActivity()).getSupportActionBar().setTitle("Expenses");
    // override on back key pressed back to main fragment
    v.setFocusableInTouchMode(true);
    v.requestFocus();
    v.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if( keyCode == KeyEvent.KEYCODE_BACK ) {
                getFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
                ((AppCompatActivity)getActivity()).getSupportActionBar().setTitle("Home");
                return true;
            } else {
                return false;
            }
        }
    });

    return v;
}

I even added this line ((AppCompatActivity)getActivity()).getSupportActionBar().setTitle("Home"); to set the title when back button is on click but it is futile.

Try it this way: 1. In your activity override onBackPress:

 @Override public void onBackPressed() {
     if (count == 0) { 
         super.onBackPressed(); 
     }
    else {
         getSupportFragmentManager().popBackStack(null,FragmentManager.POP_BACK_STACK_INCLUSIVE); 
    }
 }    
  1. Remove the KeyListener of the fragment.

((AppCompatActivity)getActivity()).getSupportActionBar().setTitle("fragment title"); Just put this line in onCreateView() fragment.

and don't override back button in fragment override it in activity.

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