我的应用程序Notification从IntentService. 轻按时,Notification类型MainActivity为Activity. MainActivity's onCreate()每次Notification点击 new 时都会调用方法,无论是否MainActivity已经创建了实例。点击Notification总是会创建一个替换旧实例的新实例。
有没有办法onCreate() 只在活动不存在时调用并调用,例如onNewIntent()如果MainActivity已经存在(或其他适当的方法)?
目前Notification是这样创建的:
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(com.exampleapp.myclient.R.drawable.dummy_notification_icon, "Notification!", System.currentTimeMillis());
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(getBaseContext(), 0, notificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
notification.setLatestEventInfo(context, notificationTitle, "test", pendingIntent);
notificationManager.notify(1, notification);
MainActivity在清单中:
<activity android:name="com.exampleapp.myclient.MainActivity"
singleTop="true">
</activity>