В моем проекте мне нужно манипулировать открытыми фрагментами из MainAcitivty. Для архивации мне нужны фрагменты тегов , созданные с помощью AppBarConfiguration.Builder
для использования fragmentManager.findFragmentByTag(currentlyShownTag)
. Я использую шаблон из Android studio - Navigation Drawer Activity. В своей деятельности у меня есть 3 фрагмента - Дом, Слайд-шоу, Галерея. Код выглядит так.
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);
}
}
Я пытался использовать android:tag="fragment_home"
в activity_main_drawer. xml для элементов android:id="@+id/nav_home"
et c .. и даже в frag_home. xml, но безуспешно, потому что String tag
всегда null;
List<Fragment> fragments = getSupportFragmentManager().getFragments();
for(Fragment fragment:fragments){
String tag = fragment.getTag();
}
Как посмотреть существующий (домашний) фрагмент в отладчике
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