I have 2 activities:
HomeActivity
OtherActivity
When push arrives it suppose to start OtherActivity
by clicking on it but also loads MainActivity
in background, so when the user will press the back button (from OtherActivity
) he will be redirect to HomeActivity
.
public class ActivityHome extends AppCompactActivity
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.e(TAG, "onCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
mMainContainerLayout = (FrameLayout)findViewById(R.id.main_content);
handleLoadFromNotification();
// Loading fragment
FragmentHome fragment = new FragmentHome();
fragmentTransaction(0, fragment, animation, false, FragmentHome.class.getSimpleName());
}
private boolean handleLoadFromNotification() {
boolean isFromNotification = getIntent().getBooleanExtra("isFromNotification", false);
if (isFromNotification) {
// Enter from notification
int typeOfNotification = getIntent().getIntExtra("type", 0);
Intent intent = null;
if (typeOfNotification == GCMNotificationIntentService.NOTIFICATION_1) {
// Form notification
intent = new Intent(getApplicationContext(), ActivityForm.class);
String id = getIntent().getStringExtra("id");
if (formId != null) {
intent.putExtra("formId", formId);
}
}
if (intent != null) {
// Open activity of notification
intent.putExtra("isFromNotification", isFromNotification);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
loadActivity(true, null, intent);
}
return isFromNotification;
}
@Override
public void loadActivity(boolean supportBack, int[] animation, Intent intent) {
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// Staring activity
startActivity(intent);
if (animation != null) {
overridePendingTransition(animation[0], animation[1]);
}
// Check if need to support the back button
if (!supportBack) {
// Close this activity
finish();
}
}
}
OtherActivity
it's just another activity with some UI, nothing special in it.
Here is how I'm sending the notifications:
public class GCMNotificationIntentService extends IntentService {
private final String TAG = getClass().getSimpleName();
public static final int NOTIFICATION_CHAT = 1;
private NotificationManager mNotificationManager;
public GCMNotificationIntentService() {
super("GCMNotificationIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
// Check if user at home screen by checking the current front application activity
Activity currentActivity = ((AppController)getApplication()).getCurrentActivity();
atHome = false;
if (currentActivity != null) {
atHome = currentActivity.getClass().getSimpleName().equals(ActivityHome.class.getSimpleName());
}
Bundle extras;
GoogleCloudMessaging gcm;
extras = intent.getExtras();
gcm = GoogleCloudMessaging.getInstance(this);
messageType = gcm.getMessageType(intent);
// Check the status of the connection to gcm
if (!extras.isEmpty()) {
if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
// sendNotification("Send error: " + extras.toString());
} else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {
// sendNotification("Deleted messages on server: " + extras.toString());
} else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
Set<String> keys = extras.keySet();
String msg, type;
if (keys.contains("data")) {
try {
JSONObject json = new JSONObject(extras.getString("data"));
msg = json.getString("body");
type = json.getString("event");
if (type.equals("Form")){
// get form id
String formId;
JSONArray data = json.getJSONArray("data");
id = data.getString(0);
sendNotification(msg, NOTIFICATION_1, id);
}
} catch (JSONException e) {
e.printStackTrace();
}
GcmBroadcastReceiver.completeWakefulIntent(intent);
}
private void sendNotification(String msg, int type, String data) {
// Send the notification to the device
boolean loggedIn = ModelPatient.getInstance().getUserId(getApplicationContext()) != null;
mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = null;
// Check if user is logged in before sending push
if (loggedIn){
intent = new Intent(this, ActivityHome.class);
intent.putExtra("isFromNotification", true);
intent.putExtra("type", NOTIFICATION_1);
intent.putExtra("id", data);
}
if (!atHome && intent != null) {
notify(msg, type, intent);
}
}
}
private void notify(String msg, int type, Intent intent) {
int requestID = (int) System.currentTimeMillis();
PendingIntent contentIntent;
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK);
contentIntent = PendingIntent.getActivity(this, requestID, intent, PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.logo)
.setContentTitle("New Title")
.setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
.setAutoCancel(true)
.setLights(PURPLE, 500, 500) .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setContentText(msg);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(type, mBuilder.build());
}
}
The Issue:
When I'm doing the the following:
1.Application in background
2.Got new push
3.Click on push in the notification drawer
4.OtherActivity is shown as expected
5.Press back button (Now HomeActivity is in front)
6.Press back button again (App is in background)
7.Enter app via application stack
Now HomeActivity
should start without OtherActivity
, but OtherActivity
it shown also! - onCreate is starts with the same intent sent by notificationManager when clicking on the notification.
If in step 6, I'm exit app by clicking the home button and then go back to activity as step 7 describes everything seems to be ok.
In general the problem is that the application some how keep this intent when enter back app from the stack.
I've tried to change the PendingIntent
's and Intent
's flags but nothing seems to work.