I have come across a requirement but I am not able to get the correct way of implementation and hence need your help.
What I want to do? - I want to perform an action depending upon notification I get as follows:
- When app is open and in foreground i.e. visible to user and I get notification I simply show a popup to start my Activity B
- When app is closed i.e neither in background nor in foreground and I get the notification I will start my application first and then start Activity B
- When the app is running BUT in background i.e in recents but not visible to user then I want to start my Activity B without re-starting the application. Also, in this case when user presses back on Activity B, they should see the screen they left off before sending it to background.
What I have done? I have achieved point #1 and #2. I want to achieve point #3. I have tried the below
public static boolean isApplicationBroughtToBackground(final Activity activity) {
  ActivityManager activityManager = (ActivityManager) activity.getSystemService(Context.ACTIVITY_SERVICE);
  List<ActivityManager.RunningTaskInfo> tasks = activityManager.getRunningTasks(1);
  // Check the top Activity against the list of Activities contained in the Application's package.
  if (!tasks.isEmpty()) {
    ComponentName topActivity = tasks.get(0).topActivity;
    try {
      PackageInfo pi = activity.getPackageManager().getPackageInfo(activity.getPackageName(), PackageManager.GET_ACTIVITIES);
      for (ActivityInfo activityInfo : pi.activities) {
        if(topActivity.getClassName().equals(activityInfo.name)) {
          return false;
        }
      }
    } catch( PackageManager.NameNotFoundException e) {
      return false; // Never happens.
    }
  }
  return true;
}
However this returns true in both the cases, point #2 and #3, so I am not able to distinguish only #3.
Also I tried the below in every Activity I have,
@Override
protected void onPause() {
    super.onPause();
    saveIsPausedInPref(true);
}
@Override
protected void onResume() {
    super.onResume();
    saveIsPausedInPref(false);
}   
But, it also doesn't give the desired result coz if the app is sent to background by pressing the Home button my Preference will have isPaused = true and if the user removes the app from recent then it will stay true and again I will not be able to differentiate Point #2 and #3 when the notification arrives.
Apologies for the whole story, but I hope I am able to explain my requirement.
Thanks in advance. :)
Edit:
        <activity
            android:name=".HomeActivity"
            android:screenOrientation="portrait" >
        </activity>
        <activity
            android:name=".ChatProfileActivity"
            android:screenOrientation="portrait" >
        </activity>
 
     
     
     
     
    