Android不重用主要活动并启动多个实例(仅在使用PendingIntent之后的行为)

adr*_*mir 1 android android-pendingintent

我有一个只有一个活动的应用程序.在onCreate中我显示一个通知,点击我显示我的活动,必要时重复使用它(或者我认为)代码是:

package com.example.multiplestartupifusingpendingintent;

import android.app.Activity;
import android.app.Notification;
import android.app.Notification.Builder;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends Activity {

    public static final String TAG = "hexor";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Log.d(TAG,
                "onCreate " + getClass().getSimpleName()
                        + Integer.toHexString(hashCode()));

        NotificationManager notifMgr = (NotificationManager) getSystemService(Service.NOTIFICATION_SERVICE);
        showReuseNotification(notifMgr);
    }

    private void showReuseNotification(NotificationManager notifMgr) {
        Intent reuseIntent = new Intent(this, MainActivity.class);
        reuseIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                | Intent.FLAG_ACTIVITY_SINGLE_TOP);

        PendingIntent reusePendingIntent = PendingIntent.getActivity(this, 2,
                reuseIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        Notification.Builder builder = new Builder(this)
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentIntent(reusePendingIntent)
                .setContentTitle("Resue from memory");

        notifMgr.notify(2, builder.build());
    }

    @Override
    protected void onDestroy() {

        Log.d(TAG,
                "onDestroy " + getClass().getSimpleName()
                        + Integer.toHexString(hashCode()));
        super.onDestroy();
    }
}
Run Code Online (Sandbox Code Playgroud)

清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.multiplestartupifusingpendingintent"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="19"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.multiplestartupifusingpendingintent.MainActivity"
            android:configChanges="orientation|screenSize"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
Run Code Online (Sandbox Code Playgroud)

我的问题是发生了一种行为,我不明白:

  1. 如果我启动应用程序(首先是onCreate调用),然后将其置于backround,然后单击其桌面图标,则重复使用相同的实例.在logcat中,我只能看到1个onCreate调用.这就是我想要的

  2. 如果我启动应用程序(首先是onCreate调用),将其置于后台,单击通知,然后单击桌面图标(第二次onCreate调用),然后我在logcat中看到2个onCreate调用.当我退回时,我先退两次才能退出应用程序,通常我会看到2个onDestroy调用,因为我有2个相同的堆叠活动.

为什么最后的行为发生了?为什么Android不会重复使用现有活动,并创建2个堆叠的副本?更奇怪的是,为什么它仅在案例2中做/为什么它不在案例1中呢?

谢谢

Jun*_*dge 6

在您评论的第一种情况下,您的活动已暂停且尚未被销毁,因此它会从暂停状态转为恢复状态.您可以查看活动生命周期以了解有关它的更多信息.

你应该设置

android:launchMode="singleInstance"
Run Code Online (Sandbox Code Playgroud)

到您的活动(在清单中)并接收内部的新意图:

  • onCreate(...)以防活动被销毁
  • onNewIntent(...)以防活动暂停/恢复

希望能帮助到你.