Android设置片段工具栏标题

时间:2017-05-17 02:38:01

标签: java android android-fragments toolbar

我在片段交易方面遇到了一些小错误。当我打开多个片段并按后退按钮并且工具栏上显示的标题错误时,会出现问题。

例如,从我的Home -> Expenses fragment,我按下费用片段中的后退按钮,工具栏上显示的标题是正确的。

但是,让我们说Home -> Expenses -> Incomes fragment,然后我按下Incomes片段的后退按钮,它将返回Home片段,但工具栏上显示的标题将是'Expenses'而不是'Home'。

这是我为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;
            }
        }
    });

然后在我的每个片段中扩展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;
}

我甚至添加了这一行((AppCompatActivity)getActivity()).getSupportActionBar().setTitle("Home");来设置后退按钮点击时的标题,但这是徒劳的。

2 个答案:

答案 0 :(得分:-1)

以这种方式尝试:1。在您的活动中覆盖onBackPress:

 @Override public void onBackPressed() {
     if (count == 0) { 
         super.onBackPressed(); 
     }
    else {
         getSupportFragmentManager().popBackStack(null,FragmentManager.POP_BACK_STACK_INCLUSIVE); 
    }
 }    
  1. 删除片段的KeyListener。

答案 1 :(得分:-1)

((AppCompatActivity)getActivity())。getSupportActionBar()。setTitle(" fragment title"); 只需将此行放在onCreateView()片段中即可。

并且不要覆盖片段中的后退按钮,在活动中覆盖它。