打开最近的片段底部导航活动

时间:2018-07-11 04:57:57

标签: android

所以,我的活动有3个菜单(片段)。主页,新闻和设置。在设置中,还有另一个菜单(ListView)。配置文件,关于和联系我们。

如果单击Profil,它将打开新活动(Profil.Class)。

我的问题是如何打开“最近使用的标签”(设置),因为现在如果单击工具栏上的“后退”按钮和后退箭头,它将使用home fragment而不是s ettings framgent打开My bottom活动。我试图从我的putExtra();发送SettingFragment,但是不起作用。我该如何实现?

这是我的MainActivity

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        toolbar = getSupportActionBar();
        BottomNavigationView navigation = findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);


        Intent intent = getIntent();
        Integer tabtoOpen = intent.getIntExtra("Tab",0);

        Log.d("TABB", tabtoOpen.toString());

        if(tabtoOpen == 0){
            fragment = new MainFragment();
        }else if(tabtoOpen == 1){
            fragment =  new NewsFragment();
        }else if(tabtoOpen == 2){
            fragment =  new SettingsFragment();
        }

        loadFragment(fragment);

    }
    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.navigation_home:
                    toolbar.setTitle("Home | " +getString(R.string.app_name));
                    fragment = new MainFragment();
                    loadFragment(fragment);
                    return true;
                case R.id.navigation_news:
                    toolbar.setTitle("News | " +getString(R.string.app_name));
                    fragment = new NewsFragment();
                    loadFragment(fragment);
                    return true;
                case R.id.navigation_setting:
                    fragment = new SettingsFragment();
                    loadFragment(fragment);
                    toolbar.setTitle("Setting | " +getString(R.string.app_name));
                    return true;
            }
            return false;
        }
    };

    private void loadFragment(Fragment fragment) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.frame_container, fragment);
        transaction.addToBackStack(null);
        transaction.commit();
    }

这是我的个人资料

public void onBackPressed() {
    Intent i = new Intent(this, Activity_main.class);
    i.putExtra("Tab", 2);
    this.startActivity(i);

}

1 个答案:

答案 0 :(得分:0)

您可以使用 setSelectedItemId()设置要选择的导航项。

else if(tabtoOpen == 2){
navigation.setSelectedItem(R.id.navigation_setting);
}