I have two activities in "Recent Applications". For those that don't know, Recent Applications is when you long press the home button.
I know why this happens. I am starting my app from a BroadCastIntentReceiver. Then I start my app from the normal launcher icon.
When I press the first icon in Recent Applications, the app opens from the Main activity. However, when I press the second icon, the app opens from the place where the BroadCastIntentReceiver starts the app, this is called PictureActivity.
I have been looking into taskAffinity and am having trouble deciding the most simple way to solve this issue.
Thank you!
When your BroadCastIntentReceiver builds its intent to launch the PictureActivity, try setting this flag on the intent:
intent.addFlags( Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS );
Reference: Android dev site.
Related
I wanted a way to exit my app. Hence I searched and found a piece of code which does that.
But I am not able to understand the code and why it does what it does.
Can anyone please explain?
Here is my code:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
It's starting the action ACTION_MAIN that has the category CATEGORY_HOME. This corresponds to:
This is the home activity, that is the first activity that is displayed when the device boots.
So, this means the launcher. So, this is a fancy way of starting the launcher app.
Also, the flag FLAG_ACTIVITY_NEW_TASK means it will be started on a new task, not the same as your app is running.
Notice that this will not finish your app, it will just put it in the background (like switching to another app using the task switcher, just this app happens to be the launcher). The effect is the same as pressing the home button.
Also notice that if the user happens to have two launcher apps, and it has not selected the default one, it will get a chooser asking to select which one of the two (or more) apps wants to use. Not the best experience.
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'm working on large project, so there is some logic for saving application state, and then opening correct activity(fragment) when it comes from background.
But anyway, I've found that if user navigates through my app and then minimize it - android opens it from background in different ways in following cases:
User taps on app icon (behavior: user see home activity, doesn't matter where he was, when application was minimized)
User select app from android "recent apps" (behavior: user see exactly what he saw, when the application was minimized)
So, what is the difference between launching application from background by this two ways? I always thought, that it is the same mechanism, but, obviously, I was wrong.
Thanks for any answers
You should pay atention on the folowing docs Activity and Tasks. In short words: if user start app from recents you will receive onRestart before onStart (without onCreate it means that your app was just "suspended"). You able to save screen state using onSaveInstanceState(). But in general starting from icon and from recents - different application behaviors and you should provide proper code for this ways.
UPD
As described below root cause of unexpected behaviour was additional lunchmode attribute.
From what I experience as an Android user, both are same.
The difference we usually see is how we close the app
Press back button until app close / finish()
On this state no matter how we open the apps it will go to the main screen
Press Home button
On this state depend on the app. If the app does not handle any Activity the app will same with the first state. But if the app handle something like when onPause() the Activity then finish() the apps, then whatever you open with app icon or recent apps will provide the same result.
Correct me if I am wrong
I am developing simple home screen application. So when i press home button i can
choose between native and mine home screen app. The problem is: if i set my app as default
home screen application when i restart phone i can't enter native home screen app
because it has never started so my app stands on top off stack. How can i enter
native home screen app when i restart phone if mine is default home screen app?
I have idea:
On boot, i can check the calling intent - if it contains the Home category, i will call native home screen app. Something like this:
Intent creatingIntent = getIntent();
if (creatingIntent.hasCategory(Intent.CATEGORY_HOME))
{
creatingIntent.setPackage("com.android.launcher");
creatingIntent.setComponent(new ComponentName
("com.android.launcher",
"com.android.launcher2.Launcher"));
startActivity(creatingIntent);
finish();
}
But the problem is i don't know how can i get Component name for native home screen application, can someone help?
The goal of an home app (=launcher) is to replace the native launcher, it's weird to force the cohabitation of 2 launchers. But if you success to do something like that, when you press on the home button it will launch also the Native launcher.
To answer your question, the native launcher depends of the target device. Example : samsung doesn't use the same launcher than google, so components name will be different.
Have you tried to do a broadcast receiver which launch your app at start up ? With that, you don't have to put your apps as default home app, so you conserve the choice when you press on the Home button. However, it's not a solution if a user choose your app as default app.
Maybe you can look here How to use customized screen instead of default start screen in Android?
I'm trying to track my applications usage on my device.
Is there a way to know if an app was launched from a home screen shortcut or from the launcher?
In the logcat I see there is an ActivityManager log each time an app starts, but I can't tell if there's any difference between shortcut launch and app drawer launch.
Thanks
I don't think you can tell the difference necessarily. If you created the shortcut, you might be able to tuck an extra or something in the Intent to distinguish the cases, but if the user creates the shortcut on her own, there probably is no difference.
Bear in mind that both shortcuts and "app drawers" are constructs of the same home screen application, and some home screen applications may not have shortcuts or may use something substantially different than an "app drawer".