我有一个SplashScreen活动,它会在检查用户是否登录后启动,如下所示:

 /* New Handler to start the Home-Activity
     * and close this Splash-Screen after some seconds.*/
    new Handler().postDelayed(new Runnable(){
        @Override
        public void run() {
            /* Create an Intent that will start the Menu-Activity. */
            Intent intent = new Intent(SplashScreen.this, HomeActivity.class);
            overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
            startActivity(intent);
            progressBar.setVisibility(View.GONE);
        }
    }, SPLASH_DISPLAY_LENGTH);



初始屏幕结束时,它将启动到MainScreen活动,该活动包含片段的FramLayout和底部导航栏。
但是启动时,不是从底部导航栏中的HomeFragment开始,而是显示HomeScreen活动布局。
我想要的是SplashScreen活动结束时直接进入HomeFragment。
这里是HomeScreen活动的代码:

     bottomBarLayout.addTab(tab_home).addTab(tab_wish)
        .addTab(tab_notifications)
            .addTab(tab_profile)
                .create(new BottomBarLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(BottomTabView tab) {


                //Log.e(TAG, "onTabSelected: =="+tab.getTabPosition() );

                Fragment selectedFragment = null;

                switch (tab.getTabPosition()){
                    case 0:
                        Toast.makeText(getApplicationContext(), "Acceuil", Toast.LENGTH_SHORT).show();
                        selectedFragment = new HomeFragment();
                        break;
                    case 1:
                        Toast.makeText(getApplicationContext(), "Voeux", Toast.LENGTH_SHORT).show();
                        selectedFragment = new WishListFragment();
                        break;
                    case 2:
                        Toast.makeText(getApplicationContext(), "Notifications", Toast.LENGTH_SHORT).show();
                        selectedFragment = new NotificationsFragment();
                        break;
                    case 3:
                        Toast.makeText(getApplicationContext(), "Profile", Toast.LENGTH_SHORT).show();
                        selectedFragment = new ProfileFragment();
                        break;

                }
                getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout,
                        selectedFragment).commit();

            }

            @Override
            public void onTabUnselected(BottomTabView tab) {

            }

            @Override
            public void onTabReselected(BottomTabView tab) {

            }
        });
}



和FragmenHome:

public class HomeFragment extends Fragment {

public HomeFragment() {
// Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                     Bundle savedInstanceState) {

// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_home, container, false);
}
}

最佳答案

因此,我在MainsScreen活动中解决了使用此简单方法遇到的问题:

private void replaceFragment(Fragment fragment){
    FragmentManager manager = getSupportFragmentManager();
    FragmentTransaction transaction = manager.beginTransaction();
    transaction.replace(R.id.frame_layout, fragment);
    transaction.commit();
}


并在onCreate中这样调用它:

replaceFragment(new HomeFragment());


HomeFragment是飞溅初始时我要移动的片段,现在它结束时,它直接进入底部导航栏上的firs选项卡。

10-06 13:18