I have a app that sets the main Actvity as the main launcher. How can I prevent my current foreground Activity from closing whenever I press the Home Button which calls the background activity from the bottom of the stack. Is there a way to trap the intent and allow the the foreground ativitiy to finish. My manifest has the following:
android:launchMode="singleTask"
There is no way within the public APIs to "override" the home button in the manner that you wish.
Related
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.
I have an app where my LAUNCHER activity, named dispatch activity, which sees whether the user has logged in before or not(in onCreate()), and then launches the correct activity. And when the correct activity is launched, I can tap the back button on my device and it goes back to the dispatch activity, and it stays there until the user refreshes the app. I only want the activity open-able by LAUNCHER. So if the user clicks the back button on the device, it stays there, like the dispatch activity doesn't exist. (By the back button I mean by that icon next to the home button on the tablet)
Hope you can help
Have you tried this : when you start the second activity, at the same time you exit (run finish()) on your dispatch activity?
EDIT: like this: (in your Dispatch Activity)
//quit your Dispatch Activity
finish();
//start new activity
startActivity(new Intent(this, YourNewActivity.class));
I'd recommend to play with intent flags. In this particular case, have a look at
FLAG_ACTIVITY_NO_HISTORY.
Set it for your dispatch activity right in the manifest and it won't be kept in the history stack.
Add LAUNCHER activity as a FLAG_ACTIVITY_NO_HISTORY.
Just define in your manifest file:
<activity android:name="LAUNCHER ACTIVITY"
android:noHistory="true"
...
></activity>
Now validate your user login stuff and just start your next activity. Rest of the stuff will be taken care by Android.
My app works as launcher. 10 mins later another activity starts automatically. When this activity started, if user presses home button, he returns to the main activity. However, I want to change the launcher activity as the second one. It must be forbidden to return to the main activity even if he presses home button.
Instead of playing around with the intent filters, I suggest you create a blank Activity, with no UI, and register it as your launcher.
This Activity's sole role is to choose the correct actual Activity you wanna show, launch it as a new task, and then quit silently.
I'm developing an application containing 2 activities:
Main activity launched where user starts the app.
Second activity is launched where user clicks on a button widget
on the home screen.
I created my widget and the view that will be displayed when the user clicks on the widget button.
My problem: The second activity UI is displayed above the main activity, the main activity is visible because the second activity layout is transparent, because I want to let the home screen still be visible when clicking in the widget.
How can I launch only the second activity without launching the main activity below it? I tried to remove <action android:name="android.intent.action.MAIN" /> in the main activity from the manifest file, widget works but when I install my app in the phone there's no app icon installed so I can't launch the main activity.
I hope that I have expressed my problem.
You have to finish the first Activity right after you start the second Activity
startActivity(<secondActivity>);
finish();
You can either finish your main activity after you start your second activity:
startActivity(secondActivity.class);
finish();
Or in your application manifest you can use the noHistory flag on your main activity, so it calls finish itself when you navigate away from it.
android:noHistory="true"
Perhaps having a different intent action and intent filter associated with the second activity will work. Have a look at the documentation. The notepad application example is should be similar to what want need to do.
I solved my problem like this :
1- I added to the main activity attributes in the manifest file :
android:launchMode="singleTask"
I tested it does not makes loading time when re-launching activity longer than usually because this attribute does not erase the app cache and keeps the intent instance in the memory.
2- Add this to main activity class :
#Override
void onPause()
{
super.onPause();
finish();
}
Thank you all for your help.
My app consists mainly of two activities, A and B, and a background service (started by alarms).
Activity A is the launcher (main) activity.
Activity B is started every now and then by the background service, with FLAG_ACTIVITY_NEW_TASK set (this is required for activity starts in bg services).
Desired behaviour: Whenever the app is left (return to homescreen, backbutton etc.) and the user returns to it, Activity A shall be resumed.
My problem: Whenever B has been started by the background service, the app will always return to B through the activity switcher (home-button long press), no matter if I have closed it via back-button or not. I can then only return to Activity A if I select the home icon of the app (because A is then started as launch activity).
I think this has to do with fact that B is started in a new task by the background service, and the app-switcher tries to return to this new task. But shouldn't the new task end when I close B via back-button, returning to A?
Can someone please explain to me what's the matter here?
The full and final solution is to add the below code to the activity in the menifest file which is opened by the service.
android:taskAffinity=""
android:excludeFromRecents="true"
android:launchMode="singleInstance"