Navigation Drawer - add tags to the fragments

Viewed 514

In my project i need to manipulate with opened fragments from MainAcitivty. To archive that, i need tag fragments created with AppBarConfiguration.Builder to use fragmentManager.findFragmentByTag(currentlyShownTag). I am using template from Android studio - Navigation Drawer Activity. In my activity i have 3 fragments - Home, Slideshow, Gallery. Code look out like this.

public class MainActivity extends AppCompatActivity {
private AppBarConfiguration mAppBarConfiguration;
private DrawerLayout drawer;
...
 @Override
protected void onCreate(Bundle savedInstanceState) {
...
drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
mAppBarConfiguration = new AppBarConfiguration.Builder(
            R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow)
            .setDrawerLayout(drawer)
            .build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);  
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
 }
}

I tried use android:tag="fragment_home" in activity_main_drawer.xml on items android:id="@+id/nav_home" etc.. and even in fragment_home.xml but without success, because String tag is always null;

 List<Fragment> fragments = getSupportFragmentManager().getFragments();
  for(Fragment fragment:fragments){
       String tag =  fragment.getTag();
    }

How look out existing (home) fragment in debugger

fragments = {ArrayList@12920}  size = 1
 0 = {NavHostFragment@12923} "NavHostFragment{f63acc7 #0 id=0x7f080081}"
  mDefaultNavHost = true
  mGraphId = 2131623936
  mNavController = {NavController@12983} 
  mAdded = true
  mAnimationInfo = null
  mArguments = null
  mBackStackNesting = 0
  mCalled = true
  mChildFragmentManager = {FragmentManagerImpl@12984} "FragmentManager{d436dbf in NavHostFragment{f63acc7}}"
  mChildNonConfig = null
  mContainer = null
  mContainerId = -1
  mDeferStart = false
  mDetached = false
  mFragmentId = 2131230849
  mFragmentManager = {FragmentManagerImpl@12921} "FragmentManager{d9baa1d in HostCallbacks{88a7492}}"
  mFromLayout = true
  mHasMenu = false
  mHidden = false
  mHiddenChanged = false
  mHost = {FragmentActivity$HostCallbacks@12985} 
  mInLayout = true
  mIndex = 0
  mInnerView = {FrameLayout@12986} "android.widget.FrameLayout{1e01c8c V.E...... ........ 0,0-720,1120 #7f080081 app:id/nav_host_fragment}"
  mIsCreated = true
  mIsNewlyAdded = false
  mLayoutInflater = {HwPhoneLayoutInflater@12987} 
  mLifecycleRegistry = {LifecycleRegistry@12988} 
  mMenuVisible = true
  mParentFragment = null
  mPerformedCreateView = true
  mPostponedAlpha = 0.0
  mRemoving = false
  mRestored = false
  mRetainInstance = false
  mRetaining = false
  mSavedFragmentState = null
  mSavedUserVisibleHint = null
  mSavedViewState = null
  mState = 4
  mTag = null
  mTarget = null
  mTargetIndex = -1
  mTargetRequestCode = 0
  mUserVisibleHint = true
  mView = {FrameLayout@12986} "android.widget.FrameLayout{1e01c8c V.E...... ........ 0,0-720,1120 #7f080081 app:id/nav_host_fragment}"
  mViewLifecycleOwner = {Fragment$3@12989} 
  mViewLifecycleOwnerLiveData = {MutableLiveData@12990} 
  mViewLifecycleRegistry = {LifecycleRegistry@12991} 
  mViewModelStore = null
  mWho = "android:fragment:0"
  shadow$_klass_ = {Class@12427} "class androidx.navigation.fragment.NavHostFragment"
  shadow$_monitor_ = -1889293113
0 Answers
Related