How to prevent custom home launcher app restart activity? - android

I am working on my own home launcher replacement and it works fine but one thing bothers me. When I press a home key, current home activity (the one defined in manifest as main/defualt/launcher/home) restarts - current activity instance onpause is executed and oncreate is fired again, so new activity is brought up.
On the other hand, ADW launcher and LauncherPRo does not behave like that - I do not a refresh like in my case. Launcher Pro even can do several actions:
If you are on the main screen with app icons, it zooms out to see a snapshot of all screens,
If you open a drawer and press Home, it just go back to the main screen.
Any ideas how to do that?
I just did a very simple prototype from scratch with just one activity (defined in manifest as main/defualt/launcher/home) and I see the same thing - it gets recreated if I press Home.

Add
if (!isTaskRoot()) {
finish();
return;
}
to the onCreate() of your first Activity (see Android application restarts when opened by clicking the application icon).

Add android:launchMode="singleInstance" to your <activity> element in the manifest.

Related

Android skipping the splash screen when re-opening the app after having pressed back

So my scenario is as such.
Let's say there is a MainActivity, which only job is to start, call installSplashScreen().setKeepOnScreenCondition { true } to show the Splash screen using the new backward compatible APIs, and then after checking some state it does startActivity(SomeActivity); finish()
Now we're on the SomeActivity and if we press the home button, the app is gone on the background. Then if we click on the launched icon, the SomeActivity is launched correctly, and the MainActivity's onCreate is never called, therefore the splash screen does not show again, and the SomeActivity shows instantly.
But if instead of pressing the home button, we press the back button, and the app is backgrounded that way, then when we click on the launcher icon, the MainActivity's oncreate is called again, and the splash screen icon flashes for a tiny fraction too making it look jarring.
My question is, does this sound like it's some wrong configuration on my part, or am I stuck with this behavior as long as I am not on a single activity architecture?
You are confused. Pressing the BACK button does not "send the app to the background". The default behaviour of the BACK button (assuming that you don't override this and provide your own behaviour) is to finish the current Activity. Normally this will take the user to the previous Activity in the current task. In your case, there is no other Activity in the task stack, so the current task is empty. This may appear to the user as "the app is sent to the background", but in reality there is nothing in the background. Your app's task is empty and so it is gone.
When the user then taps the app icon on the HOME screen or taps on the app's task in the list of recent tasks, there is no existing task to bring to the foreground, so the app is started again by creating a new task and launching the root Activity (in your case MainActivity) into the newly created task.
If you want the BACK button to just put your app into the background (instead of calling finish() on SomeActivity, which is the default behaviour) then just override onBackPressed() in SomeActivity and do this instead:
moveTaskToBack(true);
It seems like there is no solution to what I am facing as long as the Activity I want to resume to is not the Launcher activity.
From the the docs:
"Root launcher activities are activities that declare an Intent filter with both ACTION_MAIN and CATEGORY_LAUNCHER. These activities are unique because they act as entry points into your app from the app launcher and are used to start a task.
System behavior on Android 12 and higher
The system moves the activity and its task to the background instead of finishing the activity. This behavior matches the default system behavior when navigating out of an app using the Home button or gesture."
Reading the docs about the new back behavior on Android 12 and onwards tells us that pressing back when you got nothing else on the stack will act as if you pressed the home button.
There's a big exception here, and that is that when you re-open the application, if the one you just popped was not the launcher activity, it will then still need to launch that launcher activity and can't resume from where you left off in a hot state, exactly the reason why I am seeing the splash screen again.
So I think my best bet is to either ignore this for now, or fix my app to be a single-activity app, or at least keep the launcher activity be the top-level one that you exit the app from by pressing back
To indicate a couple of examples, if one wants to experience what I mean, the reproduction steps are to:
Open the app
Press the back button which will send you out of the app to the home screen
Click on the app icon again
As of today, apps like Google Photos, and Google Podcasts don't show the splash again. In contrast, apps like Google Maps, Twitter, Spotify do show the splash again for a brief second.
to call the launcher activity every time you have to clear the stack that means you have to use a flag in your manifest to tell your app not to keep activity in background try android:launchMode="singleTask" inside your activity tag in manifest the activity that you want to be killed everytime you go to background, and as far as how much time splash should be showing you can use timer for that after the timer is finished then your someActivity will be called.

How to configure the back stack when activity is launched by BroadcastReceiver?

I have an app with MainActivity.java which has a button that when clicked launches Main2Activity.java. Main2Activity.java is added to the project in Android Studio using New/Activity/Basic Activity. It has a back arrow at the top and touching it will return the app to MainActivity. AndroidManifest.xml has the following for Main2Activity:
android:parentActivityName=".MainActivity"
The app also has a BroadcastReceiver which, after performing its work, launches Main2Activity.java. When started this way (when the app is in the background), the back arrow in Main2Activity does not go back to MainActivity but instead exits the app.
I would like the back function of Main2Activity to always go to MainActivity. According to Navigate up with a new back stack, there seems a way to do this, but the example is meant for an activity launched by another app.
How do I set the back destination for an activity launced by a BroadcastReceiver?
There are two ways of doing this:
Providing the Up Navigation as mentioned in the link quoted by you in the question. It should work even when you're navigating to the Activity through a broadcast.
Use startActivities from the BroadcastReceiver to start both MainActivity and Main2Activity. This allows you to create a backstack when starting an Activity and it's parent is not on the stack.
I prefer approach 2 simply because it works even when you press the hardware back, not just the back button on the Action Bar. For achieving that with approach 1, you'd also have to override onBackPressed.

Android app navigates to launch activity when coming from background

I have 3 activities ( say A, B, C were A-is the Launch activity). When I press home button when at activity C, app goes into background. After that, I took my app through all apps list menu. At that time, my launch activity is showing (activity A). When I press back button, It goes to previous activity (C). I want to retain in the same activity (C) while coming back from background. When I run application through Eclipse, It works fine. But when I send Apk file through mail and run it in device, It fails (previous problem occurs ).
I tried with
android:launchMode="standard"
AND
android:alwaysRetainTaskState="true"
in my launch activity (Login activity or A). Any body please help me, Thanks in advance.
Follow following steps to insure that you are following the right practice:
1. Make sure you are using finish(); on the Activity A or B if you want to finish it and dont if you want the back button functionality.
2. Try Implementing onpause() and onresume() even if you are not going to perform any functionality in them. Use super() for them there.
3. Also, in android when you start an activity by clicking on the icon instead of resuming it from already running activities, it exhibits a different behaviour.

Relaunching Activity again?

I have three activities A->B->C.when i am in B activity i click the home button and put the application to back ground(B->home).
When clicking on the my application icon in the home screen the Activity A is opened not activity B .but upon long pressing the home key,its showing that my app is running in background and clicking the icon open the Activity B as expected.
What was the problem?i didn't handle any home key event.How to prevent relaunching my application.
When you make a long press the dialog shows recently used app and not the ones ruuning at the moment. To make sure your activity is not killed go toSettings-> Applications-> Manage Applications -> Running tab.
Usually the behavior you described is cause by launchMode attribute in the manifest.
Check that you do not have anything there.
read more about "launchMode" attribute here.

Weird Home button behavior

I'm experiencing kind of strange behavior of my application after hard Home button is pressed.
When you press Home, everything is OK - my app goes to the background, showing Home screen. But if you try to choose my app in the main menu or in the list of last tasks it behaves like it was not started before and does not show the last activity you were on - it just starts from scratch, namely, shows the splash screen and starts next corresponding activities. Moreover, old activities of this app remain on the activities stack, and previous instance of the app is not terminated - so if you press Back for a few times you'll just run into those activities which were undoubtedly started during the previous session of work with my app. Splash screen activity is filtered by "android.intent.action.MAIN" filter and "android.intent.category.LAUNCHER" category.
The strange thing is that all of that happens despite the fact that I do not intercept any Back key hits, or override any onPause or onResume methods. What's happening contradicts with my understanding of Android app lifecycle - I was sure that when you hit Home an app just goes to the background, and when you choose it in the menu later - it just unwinds and does not start anew. (Of course, unless stuff like that is stated in the app manifest or corresponding methods are overridden or something else).
I also checked it for some other lifecycle events - such as changing orientation or flipping hard keyboard out - and none of those led to such strange results. It appears that the problem occurs when you try to start the app from main menu or menu of last applications.
I hope you will be able to help me. Any advice on what to pay attention to or where to search for solution would be really great.
Regards, Alex
You need to set android:launchMode="singleTask" in your LAUNCHER activity in your manifest file.
For more info on the launchMode attribute see here
Note that:
The default mode is "standard".
and:
Every
time there's new intent for a
"standard" activity, a new instance of
the class is created to respond to
that intent.

Categories

Resources