我有一个只有一个活动的应用程序。在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();
}
}
清单:
<?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>
我的问题是行为发生,我不明白:
如果我启动应用程序(首先是onCreate调用),然后将其置于backround,然后单击其桌面图标,则重复使用相同的实例。在logcat中,我只能看到1个onCreate调用。这就是我想要的
如果我启动应用程序(首先是onCreate调用),将其置于后台,单击通知,然后单击桌面图标(第二次onCreate调用),然后我在logcat中看到2个onCreate调用。当我退回时,我先退两次才能退出应用程序,通常我会看到2个onDestroy调用,因为我有2个相同的堆叠活动。
为什么最后一次发生这种行为?为什么Android不重复使用现有活动,并创建2个堆叠的副本?更奇怪的是,为什么它仅在案例2中进行/为什么它不在案例1中呢?
由于
答案 0 :(得分:6)
在您评论的第一个案例中,您的活动已暂停且尚未销毁,因此它会从暂停状态转为恢复状态。您可以查看活动生命周期以了解有关它的更多信息。
你应该设置
android:launchMode="singleInstance"
到您的活动(在清单中)并接收新的意图:
希望它有所帮助。
答案 1 :(得分:1)
如果您只有一个Activity
,则应该可以在showReuseNotification()
中执行此操作:
Intent reuseIntent = new Intent(this, MainActivity.class);
reuseIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent reusePendingIntent = PendingIntent.getActivity(this, 2,
reuseIntent, PendingIntent.FLAG_UPDATE_CURRENT);
这会将您应用程序的现有实例带到前台(如果有),或者启动一个新应用程序(如果还没有活动实例)。
答案 2 :(得分:0)
在第二种情况下,您的活动仍然可见,但不在前台,因为通知栏使用在第二个活动之上堆叠其自己的透明活动的技巧,使其看起来好像状态栏正在获取拉下了活动。
由于这种特殊情况,Android会为该前景活动所基于的活动分配不同的优先级。
让我来看看我所说的内容。我很快回来。