I have 4 Activities: Launcher, MainActivity(SingleTask), NotificationActivity, ExampleActivity.
When the user clicks on the notification, NotificationActivity opens then ExampleActivity and finishes the NotificationActivity.
When I press back MainActivity opens. Now if I press back, the application gets closed. If the user then opens the app from the recent Tasks, NotificationActivity opens then ExampleActivivty. But if the user opens the app from the icon, Launcher Activity opens then MainActivity.
How to handle this situation?
Exactly this problem is covered in my answer to this question:
Remove data from notification intent
When you select your app from the list of recent tasks, Android remembers how the app was originally launched (in your case, via Notification) and starts the app again the same way. This can be considered a bug or a a feature, depending on what you expect to happen.
I've got a few suggestions in my answer that you can use to deal with it.
Related
I am trying to figure out how recent apps interacts with android back button event.
When my app is not opened in the background, if I launched an deeplink and it goes to my simply destination activity. When I clicked on back button, I can see that the activity popped from stack and my app closed due to the activity was the only one in task stack.
my questions,
Even with back button clicked, and the app closed. I still see my app in "recent apps", my impression is that such back button should killed my app since no activity in stack at all. Why still in recent apps?
If I click on the app in recent apps, I observed the same intent was triggered ago (deeplink intent), and why is that? (android's mechanism to cache last intent maybe?)
In my app, it starts with the Splash Screen. Then the user login and already reach to Dashboard activity. I finish the Splash and Login activities from the backstack.
But when the user open the app again from Zapya, the app load new splash activity on top of the existing activity stack.
When the user tap on Back button, the splash activity disappears and the Dashboard activity display again.
According to my knowledge, the app should normally show the topmost activity when it already exists in the memory.
Please, suggest me is there anythings should I modify or configure?
Edit: This is a known bug and there is various ways to solve this problem. I've solved this problem by this stackoverflow answer.
You can also learn in the following post too.
App restarts rather than resumes
I'm working on a launcher app that basically launches other installed apps on the device with an explicit intent and I have a edge case scenario:
An Activity (Act) creates an intent of an application (App) and starts it by calling startActivity(intent).
App get launched, my Activity going to "stop" state.
After a while I want to get back to my application so I click on "back" hard button that closes App and bring my Application to foreground (resume state).
This is the wanted behaviour.
Here is the edge case:
If I click on the "recent applications" hard button (square icon) while on App is launched, history stack is lost, and when I return to App, and click on "back" hard button - App exists to the Launcher screen and onResume of my application is being called.
I searched the web for a solution for couple of hours now, maybe I'll find a solution here.
Thanks
It seems to me you should set android:alwaysRetainTaskState true in your root activity.
I would like the distinguish the event where a user resumes my app from the "recent apps" list, or from the launcher icon again even if there is an unfinished app running in the background.
My app is a single Activity containing multiple Fragments, the intended behavior is that if the user resumes from "recent apps", I wouldn't have to do anything as the default behavior brings them back to the fragment they left it at. However, when the user launches the app from the icon again, I want to pop all the fragments in my fragment manager to the first fragment, essentially behaving as if starting the app fresh.
Right now though, even if the user taps on the launcher icon, the app will be resumed as usual like from the "recent apps" list without onCreate() being called on the Activity.
I've looked at similar questions, but they seem to indicate that the activity DOES get recreated when the user launches from the icon again, which is weird and not what I observe: Android resume app from recent applications list
Any ideas?
You can try adding this attribute to your manifest file for your activity.
<activity>
...
android:launchMode="singleTask"
</activity
I'm building an Android app that handles incoming events.
When I receive 1 such event, if my app is in the foreground, I immediately generate an AlertDialog that requires the user's attention.
However, when the app is in the background, the user does not see this, even though the AlertDialog has already been generated. If I manually go back to my app, I see the AlertDialog and I'm able to click OK or cancel.
I tried generating a notification dialog that appears when my app is in the background. This works, and I put the PendingIntent as MainActivity. When I tap on the notification, it brings me back to my app. However, the AlertDialog isn't there anymore.
Comparing the logcats of tapping on the notification vs manually going back to my app, I notice several differences.
When I manually go back to my app, I see MainActivity is resumed immediately. However, when I tap on the notification, it first goes back to the constructor of the class that generated the notification, a few other classes/fragments, before finally resuming MainActivity.
Does anyone know why this might be the case?
Thanks.
The default behaviour of a foreground notification is to re-launch the activity. Try setting your intent flag for the PendingIntent to use this:
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
And that will then have the same behaviour as if you navigated to the activity through your app.
From docs:
If set, the activity will not be launched if it is already running at the top of the history stack.
it seems that,you are is creating new instance of app when resuming from notification
so,add this in manifest,it might help:
android:launchMode="singleInstance"
in other case it resumes hence dialog going to be persisted.