Application context as a static reference in an Activity

from the CommonsWare Community archives

At January 18, 2019, 3:04pm, Raptor asked:

As you can probably tell from the title, I have (yet another) static reference in one of the projects that I took over, this time it’s a public static Context cont in one of the activities that is initialized in onCreate(): cont = getApplicationContext();

This “cont” is then used in an Interactor to start an activity using a static method:

ChatActivity.startActivity(UserListingActivity.cont,user.email, FirebaseConn.senderUidNotif,FirebaseConn.senderNameNotif,user.firebaseToken);

Looking through the code, it would be quite a bit of work to get rid of this static reference. So I have two questions:

  1. Is it imperative to get rid of this static reference to prevent memory leaks, considering that it’s pointing to the application context which is a singleton anyway (I’m talking purely from a practical perspective, not code beauty)
  2. If I do have to get rid of it, can you recommend any way of easily obtaining the application context in a non-framework class (like an Interactor that doesn’t have a context)?

Thanks!


At January 18, 2019, 3:19pm, mmurphy replied:

No, insofar as there is no memory leak.

However, your specific use of Application isn’t great, assuming that the static ChatActivity.startActivity() method does “what it says on the tin” and starts an activity. Ideally, you start an activity from an Activity context, so everything with your task works as expected. Starting an activity from a non-Activity context requires FLAG_ACTIVITY_NEW_TASK and is really designed only for special scenarios (e.g., starting an activity in response to a Notification action click). Perhaps that’s what is going on here — I can’t tell from the isolated code snippet.


At January 18, 2019, 3:35pm, Raptor replied:

Yes I was reading about that just now. I don’t know too much about this app as I just “took it over”, but this is how that method looks like:

  public static void startActivity(Context context, String receiver, String receiverUid, String receiverName, String firebaseToken) {
Intent intent = new Intent(context, ChatActivity.class);
intent.putExtra(Constants.ARG_RECEIVER, receiver);
intent.putExtra(Constants.ARG_RECEIVER_UID, receiverUid);
intent.putExtra(Constants.ARG_RECEIVER_NAME, receiverName);
intent.putExtra(Constants.ARG_FIREBASE_TOKEN, firebaseToken);
if (FirebaseService.conversation) {
  intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  FirebaseService.conversation = false;
}
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}

It seems like your suppositions were correct.


At January 18, 2019, 3:47pm, mmurphy replied:

If that ChatActivity.startActivity() is being called in situations where an Activity is unlikely to exist (e.g., a PendingIntent triggered by a Notification), then what is there is fine, other than the duplicate intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) call. However, I wouldn’t use this for ordinary activity-to-activity navigation.


At January 18, 2019, 3:49pm, Raptor replied:

I appreciate the insight, but the only thing I actually care about (for this project) is if this will be buggy or not.