How to Get References for All Currently Active Fragments in an Activity

Is there a way to get references for all currently active fragments in an Activity?

Looks like the API currently misses a method like "getFragments".

However using the event "onAttachFragment" of class Activity it should be quite easy to do what you want. I would do something like:

List<WeakReference<Fragment>> fragList = new ArrayList<WeakReference<Fragment>>();
@Override
public void onAttachFragment (Fragment fragment) {
fragList.add(new WeakReference(fragment));
}

public List<Fragment> getActiveFragments() {
ArrayList<Fragment> ret = new ArrayList<Fragment>();
for(WeakReference<Fragment> ref : fragList) {
Fragment f = ref.get();
if(f != null) {
if(f.isVisible()) {
ret.add(f);
}
}
}
return ret;
}

In case there's no ready method to read the state from the object (isActive() in the example), I would override onResume and onPause to set a flag (could be just a bool field).

That's already some own bookkeeping, but still very limited in my opinion considering the quite specific goal you want to achieve (status dependent list).

How to get currently visible fragment from activity when using holoeverywhere slider addon?

Get a reference to the currently visible and active Fragment from the Activity when using slider addon in combination with tabber:

You can provide a tag for a Fragment like this:

sliderMenu.add("tab2", Fragment2.class, SliderMenu.GREEN).setTag("mynavigation-2");

Also you can get the current page number using:

sliderMenu.getCurrentPage();

You have no way of obtaining the current Fragment directly, but you can combine these two methods and find a Fragment via the FragmentManager:

getSupportFragmentManager().findFragmentByTag("mynavigation-" + sliderMenu.getCurrentPage());

Get a reference to the TabsTabsSwipeFragment.java from the Activity/other Fragments and dynamically disable/enable swiping.
In your code:

getSupportFragmentManager().findFragmentByTag("mynavigation-2").getChildFragmentManager().findFragmentById(tagFragmentId);

In your Activity:

private int tagFragmentId;
public void setTagFragmentId(int i) {
Log.i(TAG, "fetched setTagFragmentId: " + i);
tagFragmentId= i;
}

In your tab Fragment (which is a child of TabsTabsSwipeFragment):

   public void onViewCreated(View view, Bundle savedInstanceState) 
((mainActivity) getSupportActivity()).setTagFragmentId(this
.getId());
}

Reference: https://github.com/Prototik/HoloEverywhere/wiki/Addon:-Slider

How to get current fragment from MainActivity

You can get your current fragment like this:

if (getFragmentManager().getBackStackEntryCount() > 1) {
Fragment f = getFragmentManager().findFragmentById(R.id.content_frame);
if (f instanceof BlankFragment) {
// Do something
}
}

Get info of current visible fragment(s) in android dumpsys

Check documentation with adb shell dumpsys activity -h. You can provide a <COMP_SPEC> parameter, adb shell dumpsys activity <COMP_SPEC>. When you give the <COMP_SPEC> parameter, you get more information on the specific component, including the visible fragments and views.

Example when Android device settings are displayed:

$ adb shell dumpsys activity com.android.settings

On my device, the command output contains:

...
Active Fragments in 13c3a270:
#0: DashboardSummary{186a79e9 #0 id=0x7f0e017b}
mFragmentId=#7f0e017b mContainerId=#7f0e017b mTag=null
mState=5 mIndex=0 mWho=android:fragment:0 mBackStackNesting=0
mAdded=true mRemoving=false mResumed=true mFromLayout=false mInLayout=false
mHidden=false mDetached=false mMenuVisible=true mHasMenu=false
mRetainInstance=false mRetaining=false mUserVisibleHint=true
mFragmentManager=FragmentManager{13c3a270 in Settings{ef6d7d6}}
mActivity=com.android.settings.Settings@ef6d7d6
mContainer=android.widget.FrameLayout{9b1166e V.E..... ........ 0,0-768,1022 #7f0e017b app:id/main_content}
mView=android.widget.ScrollView{1c50410f VFED.V.. ........ 0,0-768,1022 #7f0e005a app:id/dashboard}
Child FragmentManager{2298759c in DashboardSummary{186a79e9}}:
FragmentManager misc state:
mActivity=com.android.settings.Settings@ef6d7d6
mContainer=android.app.Fragment$1@167cba5
mParent=DashboardSummary{186a79e9 #0 id=0x7f0e017b}
mCurState=5 mStateSaved=false mDestroyed=false
...

Get the current fragment object

Now at some point of time I need to identify which object is currently there

Call findFragmentById() on FragmentManager and determine which fragment is in your R.id.frameTitle container.

If you are using the androidx edition of Fragment — as you should in modern apps — , use getSupportFragmentManager() on your FragmentActivity/AppCompatActivity instead of getFragmentManager()

How I can retrieve current fragment in NavHostFragment?

Reference to the displayed fragment (AndroidX):

java

public Fragment getForegroundFragment(){
Fragment navHostFragment = getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment);
return navHostFragment == null ? null : navHostFragment.getChildFragmentManager().getFragments().get(0);
}

kotlin

val navHostFragment: Fragment? =
supportFragmentManager.findFragmentById(R.id.nav_host_fragment)
navHostFragment?.childFragmentManager?.fragments?.get(0)

Here nav_host_fragment is an ID
of the fragment tag in your activity_main.xml with android:name="androidx.navigation.fragment.NavHostFragment"



Related Topics



Leave a reply



Submit