I have an app-wide utility class AppUtils, where I can publish app relevant information at any rate:
- either as a SnackBar, when I am in the app
- 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 ?