Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect when a fragment appears on the screen?

How could some part of my code be aware of Fragment instance become visible on a screen?

The following snippet will explain my question.

public class MyApp extends Application {
public static final String TAG = MyApp.class.getSimpleName();

@Override
public void onCreate() {
    super.onCreate();
    registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
        ...

        @Override
        public void onActivityResumed(Activity activity) {
           Log.d(TAG, activity.getClass().getSimpleName() + " is on screen");
        }

        @Override
        public void onActivityStopped(Activity activity) {
            Log.d(TAG, activity.getClass().getSimpleName() + " is NOT on screen");
        }

        ...
    });
}

Here i can track when any activity within my app appears on the screen. Is there any way to extend this approach on Fragments?

Something like Activity.getFragmentManager().registerFragmentLifecycleCallbacks();

UPD. I know nothing about activities implementations, do they use fragments at all and how do they use them (injection via xml, ViewPager etc.) The only thing I have within my class is an application context. Let's assume Activity and Fragment implementations are black boxes and i am not able to make any changes.

like image 905
Johnny Doe Avatar asked Apr 06 '15 10:04

Johnny Doe


People also ask

How do you know when a fragment becomes visible?

fragment:fragment:1.1. 0 you can just use onPause() and onResume() to determine which fragment is currently visible for the user. onResume() is called when the fragment became visible and onPause when it stops to be visible.

How do you identify a fragment?

A fragment resembles a sentence in two ways. Both groups of words begin with a capital letter and conclude with an end mark—usually a period ( . ) but sometimes a question mark ( ? ) or an exclamation point ( ! ). The one important difference is that a fragment does not contain a main clause.

Which method is called once the fragments gets visible?

onStart()The onStart() method is called once the fragment gets visible.


1 Answers

Without touching the Activity or Fragment code and assuming you don't know the tag or layout it is placed in, there is very little that you can do. The best that I can see is that you could get the FragmentManager in ActivityResumed and ActivityStopped callbacks (because here you have an Activity reference) and apply a BackstackChangedListener. This assumes that you use the backstack when changing between fragments.

The issue with what you are asking is that you want lifecycle callbacks for Fragments on the Application level when you have no control over the middle men, the Activities which are already starved for Fragment callbacks. They do most everything through their FragmentManager, and propagate their own lifecycle callbacks down to the Fragments so that the fragments will behave appropriately. The onResume and onPause callbacks in fragments only occur when they are first created or when the Activity experiences those callbacks. There is only one lifecycle callback for Fragments in Activities, onAttachFragment, which if you could override, would give you references to the Fragments that are attached to the Activity. But you said you can't change the Activity or the Fragment, and you want to know when the Fragments are shown.

So if you don't use the backstack, I don't think there's a way to do what you want.

like image 115
Joey Harwood Avatar answered Sep 21 '22 16:09

Joey Harwood