0

I have implemented AdMob interstitial in my app the method works like this:

  • I construct ads object in my activity. (Ads object has reference to the activity)
  • In my ads object I check if interstitial was shown in last three minutes with timer, and if it hasn't been shown I show it through handler to that activity.
  • If activity is stoped or destroyed I also stop my ads timer

Most of the time this scenario works correctly. But sometimes the ads show up one time even if the app is already exited.

Is there a way to check from my ads object if the activity is really running and it is the current activity running on the screen ?

Datenshi
  • 1,191
  • 5
  • 18
  • 56

1 Answers1

0

With the help of below code, you can get the top activity in android :

public static boolean isThisActivityOnTop(Context context,Class<?> activityClass){
    ActivityManager am = (ActivityManager) context
            .getSystemService(ACTIVITY_SERVICE);

    ActivityManager.RunningTaskInfo taskInfo = am.getRunningTasks(1)
            .get(0);

    String topActivityClass = taskInfo.baseActivity.getClassName();

    return topActivityClass.equalsIgnoreCase(activityClass.getName());
}

NB : The code is not tested, please try it (debug).

Eldhose M Babu
  • 14,382
  • 8
  • 39
  • 44