Android, difference between LongPress Home and Home - android

In my app, I've noticed a difference when using the home button.
Ex.
I press app icon, and the app starts op like it should with Activity(A). When I press Home button, the app gets minimized as it should. When I press app icon again, the app resumes with onResume() with Activity(A)
Now for the tricky part.
From the app, when I press my "Start button" another Activity(B) is launched, and focus is changed to the new Activity(B). When I press Home, the app minimizes (like its supposed to).
And now for the problem:
If I press the app icon, my app starts op from the very beginning with Activity(A) where my start button is. (And not with ActivityB which I just minimized)
If I do the scenario over again (Start app - > focus to Activity(B) -> Press home) and this time long press the home button, and the "Recently launched" comes op, and I select my app. Now the focus resumes to Activity(B)
Both Activity(A) and Activity(B) are set to SingleTop.
My question is, why is there a difference when using the different combinations of LongPress home + icon and OneClick Home + icon

First think about this... when you are opening your application ...
your first Screen will be shown may be it is SplashScreen.
and if..you are in Activity A and goes to B and then Press BACK obviously
you will see Activity A.
So now when you Long Press Home button..That shows you recent apps with saves state of last shown Activity..that's why when you open from there..you see Activity B directly without SplashScreen.
And Simple Pressing Home Button Once,you are went to Home Screen from where you are opening Applications Screen which is Launcher Screen..from there If you press any Icon that particular app will be Launched(Means to open the app from its Main Activity that is defined in Manifest with Intent Filter set to Launcher)...That's why opening an app from there will always show you,your SplashScreen.
This is not a Bug,not an Issue..This is how the Android is designed to work.
Same button is used to perform two different events,cause they are related..but It's not like both event performs the same actions.

Continuing to the above scenario, I have an App with Login Screen which takes me to Activity A -> OnClick of some button takes me to Activity B.
when there is a LongPress of Homebutton,and I start another App and stay on it for considerable time like say 5-10 minutes. Then again When i Long Press home button, and return to my Activity - It starts with Activity B .. which is correct.
From Activity B if i press Back key, It does take me to Activity A but there is a Black screen shown. In normal cases, It shows me a a list view of Dynamic XML data.

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.

Why does "Home" button call `onDestroy()` in my Android app?

On the Android dev page, it says pressing the "Home" or "Overview" button does not invoke onDestroy,
but in my app, it keeps calling onDestroy. Are there any clues?
(detail situation below)
I've built a simple app that switches from the main activity to a second activity,
but if I press the "Home" or "Overview" button on the second activity, the onDestroy gets called.
So when I go back to my app again, it shows the main activity, not the second activity.
Is this normal?
Should I save the state if I want to go back to the last activity (not the main activity) after pressing the Home or the Overview button and coming back to my app?
Android dev page that I read:
If a new activity or dialog appears in the foreground, taking focus and completely covering the activity in progress, the covered activity loses focus and enters the Stopped state. The system then, in rapid succession, calls onPause() and onStop().
and
Note: When the user taps the Overview or Home button, the system behaves as if the current activity has been completely covered.
So it is supposed to invoke only onPause and onStop, not onDestroy, isn't it?
Finally I found the culprit!
the problem was that I set android:noHistory="true" on the second activity, in the AndroidManifest.xml file.
Making this option true let the activity not leave the history,
so if another activity comes to the foreground and the user pushes the back button, the previous activity(noHistory=true) does not show up.
Similarly, if the user pushes the Home or the Overview button, then the user tries to come back to our app, the last activity(noHistory=true) does not show up either.
You have to put the Code in your Question or we can't help you.
Maybe you are calling finish() in MainActivity after you call startActivity(MainActivity.this, SecondActivity.class) ?
Use the edit-function and show us your code then we can help you more.

Android Activity back stack and multitasking support

I have an app that supports multitasking (working in the background), however I have run into problems with the android backstack.
This is what I have:
Activity A starts Activity B for result, so...
Activity A --> Activity B
If when at Activity B the user long presses the home button and switches to another application (say the browser for example) and then long presses the home button again and comes back to my app, they will be at Activity B, however the back stack at this time will look like this:
Activity A --> Internet Browser --> Activity B
So when I do finish() to send back a result from my Activity B it does not come back to my Activity A, but rather to the Internet Browser...
This is also the case if the user doesn't use long press of the home button, but also uses the home button to come back to their launcher and then uses long press home button to come back to my app. In this case the back stack is even worse:
Home Launcher --> Activity B
So when I do finish() on Activity B, the user gets back to their home screen and they can never get back to Activity A except for if they go and start the app again from their app drawer.
Is there any way to implement multitasking work in this case? Activity B needs to always return back a result to Activity A no matter what the user opened in-between these two.
OK. After long hours of research and trying various things, here's the solution to the problem. Hopefully this helps others...
The solution is pretty straight forward and simple, in AndroidManifest.xml
set android:launchMode="singleTask" for Activity A
set android:noHistory="true" for Activity B
This way Activity B will be removed from the Stack if we go to another app like the browser or exit to the home screen, so when we come back to our app we get back to Activity A.

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.

How do I manipulate the Android activity stack?

Here's my two scenarios.
1 -
User opens app for the first time ever from android home screen
User is presented with "first time" screen (backed by first time activity, lets call it A)
User hits back button
user is returned to android home screen
2 -
User opens app for second time
User is presented with the main list screen of the application (backed by a list activity, let's call it B)
User hits back button
User is returned to android home screen
I'm already aware of numerous ways to detect whether it's the first time opening the app or not.
The problem is in having the back button return to the home screen rather than a routing activity that decides which screen to forward to.
Currently My app has an activity to decide where to route (lets call it R) the problem is, My stack either looks like R -> A or R -> B
I want A or B to replace R on the stack when they open, and if the user hits back, then they go to the android home screen, not back to R.
Having a collaborator that sets the view for A and B is also not really workable as B extends androids concrete implementation of a list Activity to get most of its functionality.
Any ideas?
I want A or B to replace R on the
stack when they open, and if the user
hits back, then they go to the android
home screen, not back to R.
Call finish() in R after it calls startActivity() to trigger the opening of A or B.

Categories

Resources