How to determine when Fragment becomes visible in ViewPager

后端 未结 26 1324
心在旅途
心在旅途 2020-11-22 00:24

Problem: Fragment onResume() in ViewPager is fired before the fragment becomes actually visible.

For example, I have 2 fragments with

26条回答
  •  不思量自难忘°
    2020-11-22 00:44

    A simple way of implementing that is checking whether user is logged in before going to the fragment.

    In your MainActivity you may do something like this inside the onNavigationItemSelected method.

     case R.id.nav_profile_side:
    
    
                    if (User_is_logged_in) {
    
                        fragmentManager.beginTransaction()
                                .replace(R.id.content_frame
                                        , new FragmentProfile())
                                .commit();
                    }else {
    
                        ShowLoginOrRegisterDialog(fragmentManager);
    
                    }
    
                    break;
    

    However, if you are using navigation drawer, the selection in the drawer will have changed to Profile though we have not gone to the ProfileFragment.

    To reset the selection to the current selection run the code below

            navigationView.getMenu().getItem(0).setChecked(true);
    

提交回复
热议问题