获取特定的片段在堆栈中

时间:2018-06-22 11:21:18

标签: java android android-fragments back-stack

我正在使用BottomNavigationViewEx。在BottomNavigationViewEx中有4个片段,并且在主要活动中也使用工具栏,但是4个不存在的片段我不希望显示工具栏,并且在返回时也希望在其他3个片段中显示工具栏,我得到片段名称但没有得到特定片段。

MainActivity

@Override
public void onBackPressed() {

    if (getSupportFragmentManager().getBackStackEntryCount() > 0) {

        String tag = getSupportFragmentManager().getBackStackEntryAt(
                getSupportFragmentManager().getBackStackEntryCount()
                        - 1).getName();
        set_fragment(tag);
        getSupportFragmentManager().popBackStack();


    } else {

        this.finish();

    }
}


public void set_fragment(String tag) {


    if (tag.equals("feed")) {

        Toast.makeText(getApplicationContext(), tag, Toast.LENGTH_SHORT).show();
        BottomNavigationViewHelper.item1 = BottomNavigationViewHelper.menu.getItem(1);
        BottomNavigationViewHelper.item1.setChecked(true);
        home_relative.setVisibility(View.VISIBLE);
        toolbar.setVisibility(View.VISIBLE);
        tx_header.setText("Feeds");



    } else if (tag.equals("noti")) {

        Toast.makeText(getApplicationContext(), tag, Toast.LENGTH_SHORT).show();
        BottomNavigationViewHelper.item1 = BottomNavigationViewHelper.menu.getItem(3);
        BottomNavigationViewHelper.item1.setChecked(true);
        home_relative.setVisibility(View.VISIBLE);
        toolbar.setVisibility(View.VISIBLE);
        tx_header.setText("Notification");


        getSupportFragmentManager().popBackStack();

    } else if (tag.equals("profile")) {
        // getSupportFragmentManager().popBackStack();

        Toast.makeText(getApplicationContext(), tag, Toast.LENGTH_SHORT).show();
        BottomNavigationViewHelper.item1 = BottomNavigationViewHelper.menu.getItem(4);
        BottomNavigationViewHelper.item1.setChecked(true);
        toolbar.setVisibility(View.GONE);
        home_relative.setVisibility(View.GONE);


        getSupportFragmentManager().popBackStack();

    } else {

        tx_header.setText("Home");
        Toast.makeText(getApplicationContext(), tag, Toast.LENGTH_SHORT).show();
        BottomNavigationViewHelper.item1 = BottomNavigationViewHelper.menu.getItem(0);
        BottomNavigationViewHelper.item1.setChecked(true);
        home_relative.setVisibility(View.VISIBLE);
        toolbar.setVisibility(View.VISIBLE);


        getSupportFragmentManager().popBackStack();
    }


}

0 个答案:

没有答案