当我在序列之后从启动器启动我的应用程序时:
SplashActivity.java
<activity
android:name=".splash.SplashActivity"
android:launchMode="singleTop"
android:screenOrientation="portrait"
android:theme="@style/SplashActivityTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
HomeActivity.java
重现问题的步骤:
应用程序未在后台运行,也未处于暂停状态。
收到通知并显示在状态栏上。
用户点击通知并启动了HomeActivity.java
。
FirebaseMessagingService.java
private void sendNotification(String messageBody, String title) {
Intent intent = new Intent(this, HomeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent =
PendingIntent.getActivity(this, 0 /* Request code */, intent, PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this).setSmallIcon(R.drawable.ic_stat_ic_notification)
.setContentTitle(title)
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setColor(ContextCompat.getColor(this, R.color.pantone_375))
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(++count /* ID of notification */, notificationBuilder.build());
}
保持HomeActivity.java
打开并从启动器启动应用程序。启动相同应用程序的另一个实例,序列为SplashActivity.java
- &gt; HomeActivity.java
。
预期结果
应用程序的一个实例应存在于任何给定点。
如果应用程序已处于打开状态且用户在HomeActivity.java
,则应该会收到焦点。
通过单击通知启动应用程序,应用程序处于打开状态。用户单击启动器图标,然后旧应用程序实例将获得焦点。
答案 0 :(得分:0)
如果您在点击Activity
时打开Notification
,请打开以下Activity
。即您的PendingIntent
将在Activity
请阅读以下comments
中所写的所有Activity
,以便了解为何已创建
public class NotificationHandlerActivity extends AppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//deep linking - resuming app code
if (isTaskRoot()) {
// This Activity is the only Activity, so
// the app wasn't running. So start the app from the
// beginning (redirect to MainActivity)
} else {
// App was already running, so just finish, which will drop the user
// in to the activity that was at the top of the task stack
Intent intent = getIntent();
Uri data = intent.getData();
//you can put your extra's if any
finish();
}
}
}
答案 1 :(得分:0)
据我了解,我想要开展每项活动的单一任务。如果我在这种情况下是正确的你必须使用android:launchMode =“singleInstance” 在activty AndroidManifest.xml文件中
或者查看本手册中的simular标志
https://developer.android.com/guide/topics/manifest/activity-element.html
电子。 G.
<activity android:name=". MainActivity"
>
>android:launchMode="singleInstance"label="@string/app_name">