Android setting fragment toolbar title

心不动则不痛 提交于 2019-12-08 10:17:32

问题


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.


回答1:


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.



回答2:


((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.



来源:https://stackoverflow.com/questions/44014584/android-setting-fragment-toolbar-title

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!