Reputation: 25
I am developing one application, which will show a list of Card View inside a Recycler view in MainActivity. While clicking the Card view it will launch different activity. The flow is SplashScreenActivity -> MainActivity(Recycler View) -> DetailsActivity The flow is working fine if we launch the application by clicking app icon.
As part of this app, I am sending FCM notifications to the app. Whenever notification is received DetailsActivity will be launched, during that time MainActivity will not be in the stack. I am staring the MainActivity whenever backbutton of Details Activity is being pressed.If DetailsActivity is not called from Notification then I am not calling MainActivity again.
Below is the code snippet I have done to handle back button in Details Activity.
@Override
public void onBackPressed() {
if (isFromNotification) {
System.out.println("Start from Notification");
Intent intent = new Intent(this,MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
super.onBackPressed();
startActivity(intent);
}else {
System.out.println("Start from Normal Mode");
super.onBackPressed();
}
}
I have two issues here.
i) When FCM is received Notification is viewed in DetailsActivity. After pressing the back button MainActivity is launched and DetailsActivity is not killed. After Pressing the back button from MaainActivity again Details Activity is launched.
ii) Whenever MainActivity is launched by after receiving notification (By Clicking back button of Details Activity), recyclerview elements are not clickable.
I tried my best to explain the issue, your help is really appreciated !!!
Upvotes: 2
Views: 1310
Reputation: 166
The PendingIntent
you include in the notification shown by your FCM message can describe a backstack with your MainActivity below your DetailsActivity.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addNextIntent(new Intent(context, MainActivity.class));
stackBuilder.addNextIntent(new Intent(context, DetailsActivity.class));
PendingIntent notificationIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
Upvotes: 2