Android设置片段工具栏标题 [英] Android setting fragment toolbar title

查看:80
本文介绍了Android设置片段工具栏标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在片段事务中有一些小错误.当我打开多个片段并按返回按钮,并且工具栏上显示的标题错误时,就会出现此问题.

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.

例如,从我的 Home->费用片段,然后按一下费用片段"中的后退"按钮,工具栏上显示的标题正确.

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

但是,假设 Home->费用->收入片段,然后从收入"片段中按后退"按钮,它将返回到首页"片段,但是工具栏上显示的标题将是费用",而不是首页".

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

这是我为MainActivity进行的设置,它扩展了AppCompatActivity:

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;
            }
        }
    });

然后在每个扩展Fragment的片段中,设置标题并覆盖后退按钮:

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;
}

我什至添加了这行代码((AppCompatActivity)getActivity().getSupportActionBar().setTitle("Home"); 来设置单击后退按钮时的标题,但没有用.

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

推荐答案

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

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

这篇关于Android设置片段工具栏标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆