According to docs,getLaunchIntentForPackage("package name") is the great way to launch any app but I think it has some disadvantage is that it launches the app perfectly but it always create new instance of that launched app or it just start the main activity of the launched app even if the app is running in background with different activity.So my question is how to use getLaunchIntentForPackage("package name") to bring the particular app to front if it is already running on our system same as we launch the running app by long pressing home button in our android phone.Is there any solution...
Related
I have an android app with a single activity which is launched with SingleInstance mode. The app has a countdown timer. When the app is minimized and goes to the background and other apps are used, I want to bring my app to front when the countdown timer finishes.
I tried:
StartActivity(Intent.AddFlags(ActivityFlags.ReorderToFront));
But it does not bring my app to front. However, it may reactivate another app which is at the top of all minimized apps.
I have tried other ActivityFlags but they don't bring my app to front when it is minimized and another app is used. How can I bring the minimized app to front when other apps are used?
My Android application is being restarted when using the launcher to launch it after I have used the app store to launch it (and visa versa). Is there any way to prevent this?
By restarted I mean that the activity stack is lost. This is important as our users are setting up and returning to an activity in the app intermittently over the course of an hour or so. After first install, they will likely have installed and opened the app from the app store, set themselves up and then backgrounded the app. Later they are likely to open the app from the launcher and lose all their state!
The problem is further compounded as we start a foreground service along with the set-up activity. Clicking the services notification should bring the user back to the set-up activity but, as with the launcher, if the user originally opened the app from the Play store, they again lose all their state!
Reproducing the problem
I've made a sample application here:
https://github.com/samskiter/LaunchTest
Note: it uses the BBC weather application package Id in order to allow you to quickly open from the app store (the "Open" button will be shown on the BBC weather application if this app is installed).
Steps are as follows:
Uninstall the BBC weather app if you have it
Install the LaunchTest app
Close the LaunchTest app from recents
Open the LaunchTest app from the BBC weather app page on the Play Store
Click the button to navigate to the Second activity
Background the application (press home)
Open the LaunchTest application from the app launcher
The state is lost! you are back at the First (root) activity
What I've tried
Using singleTask launch mode hasn't helped - it causes the app to be relaunched even if you use the launcher every time.
I've tried alwaysRetainTaskState - I don't really expect this to work as this only really affects things over about a 30 minute wait.
What I think is going on
There is no mechanism in the activity manager / intent system to open a running app in it's current state. Instead, I think the UID of the launching application is taken into account. If it is different then the Intent.FLAG_ACTIVITY_NEW_TASK flag is forced and so created a new task and dropping all my users' lovely state.
Inspecting Google Maps
Google maps has a very similar interaction model to our application: a setup UI, followed by an ongoing process the user is going through for a long period of time (navigation) with a paired, foreground service (the navigation service you can see in your notification bar). BUT GMaps doesn't suffer from this problem. I think this is because it uses only a single activity for all of it's interface and uses singleTask. So now, when you tap on the launcher after originally launching from the play store, the task can be reused.
In my opinion this exposes a hole in the android intent/activity management system. The whole point of the savedInstanceState/activity lifecycle is to prevent dropping state, but here we have a way to dump everything. My current best solution is to detect app restarts by the fact the service is running and try to get the user back to where they were, which is more than a little tricky.
If someone knows a way I can prevent my state being dropped on the floor when reopening from the app launcher after opening from the store, I would really appreciate it.
This is more like a workaround to your problem, but it seems that there may be no real solution.
My first thought was - since the whole setup will take a while anyway - why don't you just save some kind of bookmark ('firstLaunch') to Preferences, post a (delayed?) notification and finish the app, so that the user has to open it again by tapping on the launcher icon. Only then you start the real setup, so you will not lose information due to the installer vs launcher problem.
But the problem seems to have been around for some time and the following SO-posts may help:
Re-launch of Activity on Home button, but…only the first time
After tap on app icon,launcher create a new instance of root activity again & again
Hope this helps!
1.install an apk from app installer
2.then just click "OPEN" to launch it at once
3.after the app launched and then press HOME key
4.find the app from app list and click its icon to launch again
5.then the app will be launched with a new instance.
And if you repeat 3~5 several times, it will repeat create a new instance. if you press "BACK" key now, you will see the app is still there for the same times you launched.
But if you just click "DONE" at step 2 and then launch the app from app list, everything will be OK then.
Why?
The app installer (as well as many Android IDEs) use different intent flags than the regular app launcher does, which means that the launcher's intent doesn't properly match with the Activity's existing intent and it ends up creating a new activity on top of the stack.
I think this question is similar to what you're asking about:
Activity stack ordering problem when launching application from Android app installer and from Home screen
I am using Android tablet to test my application.
I press the home button to put the application to background.
Now, if
I launch the application from recent apps, it will open the same
screen, I was on, and will not restart the application.
But, if I launch the application from my apps, it starts a new
instance of application. This time it restarts the application
from splash screen, and when I check the recent apps, it shows
2 instances of my app.
I want only one instance of the application to be running, on my tablet, at any time. Please help...
I think we have three apps, and all of them are open and we know the process name. How can I switch from one app to another app, also keeping in mind that another app has opened a intent, and I do not want to go to another app with opening an app launcher, just switching or making another app at front.
I want to make another app on front, for example if Internet is background, I make it at front.
Make the main activity for your applications singleProcess, if the application is already running it will be bring to the front. you can also, implement a custom BroadCastReceiver to start the application using its package name.