我有两项活动:
HomeActivity
OtherActivity
当push到达时,它应该通过点击它来启动OtherActivity,但也会在后台加载MainActivity,因此当用户按下back按钮(从OtherActivity)时,他将被重定向到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这只是另一个带有用户界面的活动,没有什么特别之处。
以下是我发送通知的方式:
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());
    }
}

问题:
当我执行以下操作时:
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

现在HomeActivity应该在没有OtherActivity的情况下启动,但是OtherActivity也显示了!-oncreate以notificationmanager在单击通知时发送的相同意图开始。
如果在步骤6中,我通过单击Home按钮退出应用程序,然后返回活动,因为步骤7描述了一切似乎都正常。
一般来说,问题是应用程序在从堆栈中返回app时如何保持这种意图。
我试图更改PendingIntentIntent的标志,但似乎没有任何效果。

最佳答案

在步骤6中退出应用程序之间的差异。按back和home是back分别调用finish()和ondestory(),home将活动设置为onpause()状态。
http://developer.android.com/intl/es/training/basics/activity-lifecycle/recreating.html
有几种情况下,由于
正常的应用程序行为,例如当用户按下后退按钮或
您的活动通过调用finish()来表示自己的破坏。
当调用oncreate()时,otheractivity有机会启动。
尝试在gcmnotificationintentservice:notify()中使用以下标志:

int requestID = (int) System.currentTimeMillis();
PendingIntent contentIntent;
intent.setFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_TOP);
contentIntent = PendingIntent.getActivity(this, requestID, intent, 0);

09-25 21:24