Q: How to check app is open or not in android I want when a user clicked on the notification, if the app is open, open Activity1. else if the app is closed and destroyed, open Activity2.
I write this method in my Service(receive and create notifications):
private boolean isAppRunning() {
    ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningAppProcessInfo> procInfos = activityManager.getRunningAppProcesses();
    if (procInfos != null) {
        for (final ActivityManager.RunningAppProcessInfo processInfo : procInfos) {
            if (processInfo.processName.equals("package name")) {
                return true;
            }
        }
    }
    return false;
}

and use it like this:
Intent notificationIntent;

        if (isAppRunning()) {
            notificationIntent = new Intent(getBaseContext(), Activity1.class);
        } else {
            notificationIntent = new Intent(getBaseContext(), Activity2.class);
            notificationIntent.putExtra("NotificationMessage", "I am from Notification");
            notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
            notificationIntent.setAction(Intent.ACTION_MAIN);
            notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        }

        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        NotificationCompat.Builder sNotifBuilder = new NotificationCompat.Builder(getBaseContext(), offerChannelId);
        sNotifBuilder.setContentIntent(pendingIntent);

But is not working and always open Activity1. Can you help me?

A: Inherit the below BaseActivity with all the activity in the application
BaseActivity.kt
abstract class BaseActivity : AppCompatActivity() {
     private val preferences: SharedPreferences by lazy {
         PreferenceManager.getDefaultSharedPreferences(this)
     }

     override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
          preferences.isApplicationRunning = true
     }

     override fun onDestroy() {
         preferences.isApplicationRunning = false
         super.onDestroy()
     }

}

SharedPreferenceExt.kt
var SharedPreferences.isApplicationRunning: Boolean
set(value) {
    edit {
        putBoolean("APPLICATION_RUNNING", value)
    }
}
get() = getBoolean("APPLICATION_RUNNING", false)

your_service.kt
class YourService : Service() {

private val preferences: SharedPreferences by lazy {
    PreferenceManager.getDefaultSharedPreferences(this)
}

//below is the snippet of yours. changed the condition alone
private fun openActivity() {       
        if (preferences.isApplicationRunning) {
            notificationIntent = new Intent(getBaseContext(), Activity1.class);
        } else {
            notificationIntent = new Intent(getBaseContext(), Activity2.class);
            notificationIntent.putExtra("NotificationMessage", "I am from Notification");
            notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
            notificationIntent.setAction(Intent.ACTION_MAIN);
            notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        }

        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        NotificationCompat.Builder sNotifBuilder = new NotificationCompat.Builder(getBaseContext(), offerChannelId);
        sNotifBuilder.setContentIntent(pendingIntent);
       }
}


A: I hope you are looking for if your app is in foreground or not. If this is the case, you need to register ActivityLifecycleCallbacks in your application class and keep a track of onActivityStarted and onActivityStopped. If any of your activity is in started state means your app is in foreground.
Or in case you want to know if the app is completely closed in that case, it seems there is no sure shot way. Keeping a track of onActivityDestroyed is not recommended. 
There are situations where the system will simply kill the activity's hosting process without calling this method (or any others) in it, so it should not be used to do things that are intended to remain around after the process goes away as mentioned in doc. Link To Doc
