如何默认切换到底部导航栏的特定菜单项?

How to shift to a particular menu item by default for Bottom Navigation bar?

我正在构建一个带有底部导航栏的应用程序,该导航栏链接了 app:menu="@menu/bottom_menu" 文件。一旦 activity 默认启动,第一个菜单项(例如:主页图标)就会被选中。 Activity 启动后,我应该如何更改所选菜单项?

我正在使用 SharedPreferences。片段正在正确加载,但片段的相应图标未加载。默认情况下,主页片段的图标集中在底部导航栏中。

public class HomeActivity extends AppCompatActivity {

    BottomNavigationView bottomNavigationView;
    DatabaseReference reference;
    FirebaseUser user;
    public static final String MY_PREFS_NAME = "Dock";
    SharedPreferences.Editor editor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        user = FirebaseAuth.getInstance().getCurrentUser();
        editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();



        bottomNavigationView = findViewById(R.id.bottomNavigationView);
        if (savedInstanceState == null){

            SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
            int idName = prefs.getInt("idName", 1);

            if (idName == 1){
                handleFrames(new ChatFragment() );
            }
            if (idName == 2){
                handleFrames(new GroupFragment() );
            }
            if (idName == 3){
                handleFrames(new SearchFragment() );
            }
            if (idName == 4){
                handleFrames(new FriendsFragment() );
            }
            if (idName == 5){
                handleFrames(new SettingsFragment() );
            }


        }
        handleOnClickListner();
    }

    private void handleFrames(Fragment fragment) {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        // fragmentTransaction.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out);
        fragmentTransaction.replace(R.id.fragment_holder, fragment);
        fragmentTransaction.commit();
    }

    private void handleOnClickListner(){
        bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()){
                    case R.id.chat:
                        handleFrames(new ChatFragment());
                        editor.putInt("idName", 1);
                        editor.apply();
                        break;
                    case R.id.groups:
                        handleFrames(new GroupFragment());
                        editor.putInt("idName", 2);
                        editor.apply();
                        break;
                    case R.id.status:
                        handleFrames(new SearchFragment());
                        editor.putInt("idName", 3);
                        editor.apply();
                        break;
                    case R.id.friends:
                        handleFrames(new FriendsFragment());
                        editor.putInt("idName", 4);
                        editor.apply();
                        break;
                    case R.id.setting:
                        handleFrames(new SettingsFragment());
                        editor.putInt("idName", 5);
                        editor.apply();
                        break;
                }
                return true;
            }
        });
    }

试试这个 bottomNavigationView.setSelectedItemId(resId);

有了这个,您可以在脚本本身的 OnCreate 或任何其他函数中设置所选项目。