简体   繁体   English

为底部导航栏保存片段的活动到活动永远不会到达片段,而是通过 FrameLayout 保存它们的活动

[英]Activity to activity that holds fragments for bottom nav bar never get to fragment but to the activity that hold them by the FrameLayout

I have a SplashScreen Activity that launches after checking if the user is loged in, like this:我有一个SplashScreen Activity在检查用户是否登录后启动,如下所示:

 /* 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);
  • Whenever the splash screen ends, it launches to the MainScreen Activity that holds the FramLayout for the fragments and the bottom navigation bar.每当初始屏幕结束时,它就会启动到MainScreen Activity ,该 Activity 包含 Fragment 的 FramLayout 和底部导航栏。

  • But when it launches, instead to start on the HomeFragment in bottom nav bar, it shows the HomeScreen Activity Layout.但是当它启动时,而不是在底部导航栏中的 HomeFragment 上启动,它会显示HomeScreen Activity布局。

  • What i want is when the SplashScreen Activity ends it goes directly to the HomeFragment .我想要的是当SplashScreen Activity结束时它直接进入HomeFragment

  • Here the code for the HomeScreen Activity :这里是HomeScreen Activity的代码:

     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) { } }); }
    • And the the FragmenHome:还有 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); } }

So I fixed the problem that i had with this simple method in my MainsScreen activity:所以我解决了我在 MainsScreen 活动中使用这个简单方法遇到的问题:

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

and called it in the onCreate like this:并像这样在 onCreate 中调用它:

replaceFragment(new HomeFragment());

HomeFragment is the fragment that i want to go when the splash screnends, and now when it ends, it goes directly to the firs tab on the bottom navigation bar. HomeFragment 是当启动画面出现时我想要 go 的片段,现在当它结束时,它直接转到底部导航栏上的第一个选项卡。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM