单击通知时未清除 Backstack
Backstack not cleared when notification clicked
我正在使用 Firebase Cloud Messenger (FCM) 将通知推送到我的应用程序。
当应用程序在后台时会收到通知,因此不会触发 onMessageReceived,因为通知没有负载(数据)。一切都很好,但这意味着我无法创建自己的通知,因为一切都由系统托盘自动处理。
当我点击通知时,我希望整个后台堆栈被清除并且应用程序从头开始重新启动。基本上我想要 的 相反 。
这应该是默认行为。
但是,当我点击通知时,如果应用程序已经打开,应用程序会从启动器重新启动,但在现有的后台堆栈之上。
例如,如果您有:
HomeScreen -> Page1
单击通知后,堆栈中现在有:
HomeScreen -> Page1 -> HomeScreen
当它应该只是:
HomeScreen
我的启动器 Activity 仅在应用程序启动时显示,所以我不希望它保留在后台。我发现这就是为什么我会遇到这个问题。所以基本上,如果 Launcher Activity 调用 finish()
自身 and/or 在 Manifest 中设置了 noHistory="true"
,则单击通知时不会清除后台堆栈。
我该如何解决这个问题?
我找到了解决办法。想法是创建一个新的 LauncherActivity 负责启动现有的 Launcher 并清除进程中的后台堆栈。
可能还有其他方法可以做到这一点,但我想保留 noHistory="true"
的原始启动器,否则如果我实施以下解决方案,我会遇到下一个 Activity 的过渡动画问题直接给它。
新启动器名为 StartActivity
在清单中:
<activity
android:name=".StartActivity"
android:screenOrientation="portrait"
android:theme="@style/AppTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Activity:
public class StartActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = new Intent(this, LauncherActivity.class);
// Add the flags to clear the stack
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
// Start the intent with a defined transition animation. The animation is not
// required but it make the transition seamless.
startActivity(intent, getFadeInOutAnimation(this));
// Necessary for the app not to crash. Basically just a FrameLayout
setContentView(R.layout.activity_start);
}
public static Bundle getFadeInOutAnimation(Context context) {
// Allows us to display a fading animation as transition between the activities.
// The animation can be whatever you want
return ActivityOptions.makeCustomAnimation(context,
R.anim.fade_in, R.anim.fade_out).toBundle();
}
}
我正在使用 Firebase Cloud Messenger (FCM) 将通知推送到我的应用程序。 当应用程序在后台时会收到通知,因此不会触发 onMessageReceived,因为通知没有负载(数据)。一切都很好,但这意味着我无法创建自己的通知,因为一切都由系统托盘自动处理。
当我点击通知时,我希望整个后台堆栈被清除并且应用程序从头开始重新启动。基本上我想要
但是,当我点击通知时,如果应用程序已经打开,应用程序会从启动器重新启动,但在现有的后台堆栈之上。
例如,如果您有:
HomeScreen -> Page1
单击通知后,堆栈中现在有:
HomeScreen -> Page1 -> HomeScreen
当它应该只是:
HomeScreen
我的启动器 Activity 仅在应用程序启动时显示,所以我不希望它保留在后台。我发现这就是为什么我会遇到这个问题。所以基本上,如果 Launcher Activity 调用 finish()
自身 and/or 在 Manifest 中设置了 noHistory="true"
,则单击通知时不会清除后台堆栈。
我该如何解决这个问题?
我找到了解决办法。想法是创建一个新的 LauncherActivity 负责启动现有的 Launcher 并清除进程中的后台堆栈。
可能还有其他方法可以做到这一点,但我想保留 noHistory="true"
的原始启动器,否则如果我实施以下解决方案,我会遇到下一个 Activity 的过渡动画问题直接给它。
新启动器名为 StartActivity
在清单中:
<activity
android:name=".StartActivity"
android:screenOrientation="portrait"
android:theme="@style/AppTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Activity:
public class StartActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = new Intent(this, LauncherActivity.class);
// Add the flags to clear the stack
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
// Start the intent with a defined transition animation. The animation is not
// required but it make the transition seamless.
startActivity(intent, getFadeInOutAnimation(this));
// Necessary for the app not to crash. Basically just a FrameLayout
setContentView(R.layout.activity_start);
}
public static Bundle getFadeInOutAnimation(Context context) {
// Allows us to display a fading animation as transition between the activities.
// The animation can be whatever you want
return ActivityOptions.makeCustomAnimation(context,
R.anim.fade_in, R.anim.fade_out).toBundle();
}
}