Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Managing current View and Class<?> objects

I have an app-wide utility class AppUtils, where I can publish app relevant information at any rate:

  1. either as a SnackBar, when I am in the app
  2. or by means of the notification manager

For instance when the mesaages is going as a Snackbar I need the track of the current displayed View container to use it in Snackbar.make(view, text, length); When I want to publish that message by means of the NotificationManager.notify(int, Builder); and I need the Class signature in here

Intent resultIntent = new Intent(this, ResultActivity.class);

Therefore I have in my AppUtils:

public static void setCurrentViewAndClass(View v, Class<?> c)
{
    view = v; // view is static
    cls = c;  // cls is static
}

where I can remember from everywhere in my project the current view (for Snackbar parameter) and cls (for Notification Intent).

Further on, I clear those parameters, e.g. when I leave the app to the background:

public static void clearCurrentViewAndClass()
{
    view = null;
    cls = null;
}

A. When those parameters are NOT null, I know that my app has the focus with corresponding view and I can show the relevant message as a Snackbar. B. When these parameters are null, I know that my app is in the background and I want to show the relevant message as a Notification

So whenever a Fragment/Activity is created or resumed, I call setClassAndview() it in each onResume() to remember the parameters.

Is there a more elegant way to get track of the current displayed Activity or active Class ?

like image 284
Ralf Wickum Avatar asked Oct 26 '16 15:10

Ralf Wickum


2 Answers

As an example... create an Application class (MyApp) that is registered in AndroidManifest:

<application
        android:name=".MyApp"
        ...>

In MyApp application class .onCreate() set:

registerActivityLifecycleCallbacks(new MyAppActivityLifecycleCallbacks());

In MyApp, create a static field that holds a count of visible Activities and a method for determining if the app is in the foreground or not:

private static int mActivityCount = 0;

public static boolean isAppInForeground() {
    return mActivityCount != 0;
}

And finally setup your ActivityLifecycleCallbacks class for keeping count of visible Activities:

private static final class MyAppActivityLifecycleCallbacks implements ActivityLifecycleCallbacks {

        public void onActivityCreated(Activity activity, Bundle bundle) {
            // No operations
        }

        public void onActivityDestroyed(Activity activity) {
            // No operations
        }

        public void onActivityPaused(Activity activity) {
            // No operations
        }

        public void onActivityResumed(Activity activity) {
            // No operations
        }

        public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
            // No operations
        }

        public void onActivityStarted(Activity activity) {
            mActivityCount++;
        }

        public void onActivityStopped(Activity activity) {
            mActivityCount--;
        }
    }

And now you should be able to call MyApp.isAppInForeground() to determine if any of your Activities are currently in the foreground. If you need a reference to the current visible Activity you could handle that here as well.

like image 134
Jason Grife Avatar answered Oct 23 '22 08:10

Jason Grife


You can use ActivityManager for getting current Activity.

This link explains how.

However, you cannot get current Fragment since there may be multiple fragments simultaneously. You can find current fragment on a specific ViewGroup by using FragmentManager.

getActivity().getFragmentManager().findFragmentById(R.id.fragment_container);
like image 2
Efe Budak Avatar answered Oct 23 '22 09:10

Efe Budak