Going back to main activity after launching activity from notification
You should make stack builder while creating notification like this.
Intent resultIntent = new Intent(this, NotificationTapActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack
stackBuilder.addParentStack(MainActivity.class);
// Adds the Intent to the top of the stack
stackBuilder.addNextIntent(resultIntent);
// Gets a PendingIntent containing the entire back stack
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
PendingIntent.FLAG_UPDATE_CURRENT);
So whenever user will tap on notification, MainActivity will be inserted in stack.
One more solution in which you can handle the back press of NotoificationTapActivity. In which you can check if noting is there in stack then you can finish the current activity and starts MainActivity from there.
Back to main activity from notification-created activity
Aha, I simply need to use PendingIntent.getActivities()
instead of getActivity()
. Also worth mentioning is TaskStackBuilder
Here is some code:
Intent backIntent = new Intent(ctx, MainActivity.class);
backIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Intent intent = new Intent(ctx, ConversationActivity.class);
intent.putExtra("whatever", whatever);
final PendingIntent pendingIntent = PendingIntent.getActivities(ctx, UNIQUE_REQUEST_CODE++,
new Intent[] {backIntent, intent}, PendingIntent.FLAG_ONE_SHOT);
Just tested it. It works like a charm. I initially suspected that there might be a problem if the app is already running, and you go to a notification. I.e. say you activity stack is (topmost on the right):
[MainActivity] [SomeActivity]
and you click a notification for ConversationActivity
. Would you get:?
[MainActivity] [SomeActivity] [MainActivity] [ConversationActivity]
Well, it turns out you magically get
[MainActivity] [SomeActivity] [ConversationActivity]
which is what I wanted, but I have no idea how it does that. I haven't set any special options on any of the activities. Oh well!
Android Open MainActivity after click back button in Notification Activity
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);
Notification opens activity, back button pressed, main activity is opened?
Your problem is in following steps as I know from your question is
1.Create notification after boot complete
2.Main activity will call on start up
3.you pressed home button so main activity will be stopped but will not destroy
4.You click on notification from status bar so your application will be resume
so that you have already main activity in your back stack and notification will create new activity like you mentioned in your question NewNoteActivity activity will be push on back stack.SO at this step you have two activities in back stack
5.you pressed back button so that last activity will be destroy
and your main activity will be resume
But you want to go to homepage screen.
So the Your problem is in following steps as I know from your question is
1.Create notification after boot complete
2.Main activity will call on start up
3.you pressed home button so main activity will be stopped but will not destroy
4.You click on notification from status bar so your application will be resume
so that you have already main activity in your back stack and notification will create new activity like you mentioned in your question NewNoteActivity activity will be push on back stack.SO at this step you have two activities in back stack
5.you pressed back button so that last activity will be destroy
and your main activity will be resume
But you want to open homepage activity when you pressed on back button from NewNoteActivity activity.
So the solution is that when you pressed back button from your NewNoteActivityactivity you start main activity again with Intent.FLAG_ACTIVITY_CLEAR_TOP
flag so that your main activity will recreate and will receive onNewIntent()
method so there you can get flag and you can finish main activity
for example
@Override
public void onBackPressed() {
Intent i = new Intent(this, Main.class);
i.putExtra("exit", true);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
super.onBackPressed();
}
Now you have to implement onNewIntent()
method in your Main activity
On back button pressed from NewNoteActivity activity your Main activity will call onNewIntent()
method so in this method you have to fetch flag variable which is passed from NewNoteActivity activity.If you get flag and if it is true then just finish the Main activity so that you will get Home screen.
EDIT
You are saying that you have any of the activity from A,B,or C is opened and you pressed back button so that this activity will be closed.If you have only one activity in stack at that time your application will be closed means you will get home screen.But if you have more than one activity and you pressed back button you have at least one activity in stack and now you click on notification so that this will open a new activity associated with your notification so that this activity will be pushed on that on back stack.Now if you pressed back button your last activity which is associated with your notification will be closed if you have not modify onBackPressed() method in that activity and then it will check back stack if any activity in back stack so that activity will be resumed or if there is no activity in back stack then your application will be closed and you will get home screen
Notification always creates new activity instead of resuming previous activity
I've solved the problem. I needed to call the setSessionActivity() method on my media session, like so:
Intent intent = new Intent(context, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, MediaPlayerContract.REQUEST_CODE,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
mediaSessionCompat.setSessionActivity(pendingIntent);
EDIT:
you can also create an intent by simply calling
setContentIntent(controller.getSessionActivity())
after calling setSessionActivity, where controller is your MediaSessionController.
How should i do from notification back to activity without new intent
If you want to call the Activity from background try this:
Intent intent = new Intent(this, YourLauncherActivity.class);
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
intent, 0);
mBuilder.setContentIntent(pendingIntent);
If you click the Notification while on Homescreen the last shown Activity of your App will be get to the foreground without starting it new. If the Activity was killed by the system you will get a new Activity.
Related Topics
What Happens If a Android Service Is Started Multiple Times
Gesture Detection and Scrollview Issue
How to Enable and Disable a Component
Failed to Import New Gradle Project in Android Studio
"Unknown Bits Set in Runtime_Flags: 0X8000" Warning in Logcat on Android Q Emulator
How to Load HTML String in a Webview
Get the Co-Ordinates of a Touch Event on Android
Failed to Read Png Signature: File Does Not Start with Png Signature
Scoping States in Jetpack Compose
How to Use "Goasync" for Broadcastreceiver
How to Dynamically Change an App Icon Like Calendar App Does
How to Make Layouts for Several Android Screen Sizes
How to Programmatically Scroll Android Webview
Having Issue on Real Device Using Vector Image in Android. Svg-Android
How to Calculate Azimuth, Pitch, Orientation When My Android Device Isn't Flat
Android:Change Button Text and Background Color
Android Studio 4.1 Plugin Error: Plugin * Is Incompatible (Supported Only in Intellij Idea)