简体   繁体   English

Android设置片段工具栏标题

[英]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. 例如,从我的Home -> Expenses fragment ,然后从费用片段中按返回按钮,工具栏上显示的标题是正确的。

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'. 但是,假设Home -> Expenses -> Incomes fragment ,然后从Incomes片段按返回按钮,它将返回Home片段,但工具栏上显示的标题将是'Expenses'而不是'Home'。

Here is my set up for MainActivity which extends AppCompatActivity: 这是我为MainActivity设置的,它扩展了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: 然后在每个扩展Fragment的片段中,设置标题并覆盖后退按钮:

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"); 我什至添加了这一行((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: 尝试这种方式:1.在您的活动中覆盖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. 删除片段的KeyListener。

((AppCompatActivity)getActivity()).getSupportActionBar().setTitle("fragment title"); (((AppCompatActivity)getActivity())。getSupportActionBar()。setTitle(“片段标题”); Just put this line in onCreateView() fragment. 只需将这一行放在onCreateView()片段中即可。

and don't override back button in fragment override it in activity. 并且不要覆盖片段中的后退按钮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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