Is there a way to get links to all current active snippets in Activity?

I did not find an easy way to get all the current active (visible, currently resuming) fragments in Activity. Is this possible without special accounting in my business? It seems that the FragmentManager does not support this function.

+59
android android-3.0-honeycomb android-fragments
May 23 '11 at 19:33
source share
5 answers

It looks like the API is currently skipping a method like getFragments.
However, using the "onAttachFragment" event of the Activity class, it will be very easy for you 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; } 

If there is no ready-made method for reading the state from the object (isActive () in the example), I would override onResume and onPause to set the flag (there can only be a bool field).
This is already some of your own financial statements, but it is still very limited, in my opinion, given the rather specific goal that you want to achieve (a list depending on status).

+106
Aug 11 2018-11-11T00:
source share

If you use the Android Support Library, you can call the hidden FragmentManager.getFragments() method:

 public List<Fragment> getVisibleFragments() { List<Fragment> allFragments = getSupportFragmentManager().getFragments(); if (allFragments == null || allFragments.isEmpty()) { return Collections.emptyList(); } List<Fragment> visibleFragments = new ArrayList<Fragment>(); for (Fragment fragment : allFragments) { if (fragment.isVisible()) { visibleFragments.add(fragment); } } return visibleFragments; } 
+12
Dec 04 '13 at 11:56
source share

Another way to do this is to place tags on your snippets when you add them to the action.

For example, if you dynamically add 4 fragments, you can add them as:

 for (int i = 0; i < NUM_FRAGMENTS; i++){ MyFragment fragment = new Fragment(i); fragmentTransaction.add(R.id.containerId, fragment, SOME_CUSTOM_PREFIX + i).commit() } 

Then, if you need to get all the fragments later, you can do:

 for (int i = 0; i < NUM_FRAGMENTS; i++) { String tag = SOME_CUSTOM_PREFIX + i; fragmentList.add((MyFragment) fragmentManager.findFragmentByTag(tag)); } 
+2
Feb 04 '13 at 16:43
source share

android.support.v4.app.FragmentManager has a method called getFragments() that does exactly what you need, but it was never available. Suddenly, however, it is, but I am not sure if it is intended, because it is still marked as hidden.

 /** * Get a list of all fragments that have been added to the fragment manager. * * @return The list of all fragments or null if none. * @hide */ public abstract List<Fragment> getFragments(); 

The usual android.app.FragmentManager does not have this method at all, but if it is really necessary, you can access the same object by getting the mActive field through reflection (be careful: starting with API 26, its type is a SparseArray instead of List ) . Use at your own risk :)

+1
Aug 30 '16 at 15:41
source share

I decided with this:

 public ArrayList<Fragment> getAllFragments() { ArrayList<Fragment> lista = new ArrayList<Fragment>(); for (Fragment fragment : getSupportFragmentManager().getFragments()) { try { fragment.getTag(); lista.add(fragment); } catch (NullPointerException e) { } } return lista; } 
0
Jul 20 '14 at 19:47
source share



All Articles