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

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.

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.

Blank activity or fragment on reopening app after closing by pressing back button

My app get minimized when I press the back button on the home fragment, which is fine, but when I open it back up it shows me a blank activity. I guess it's not loading the fragment that it's supposed to load.
I added a text view with some text on the activity but it's not showing me that so I'm seeing something else.
How should I manage this issue?
Your activity has a lifecycle. If a lifecycle state is changing, then a method will be called. If you open your app, after you minimize your application, then the method "onResume()" will be called. Try to use this method in your activity to check, whether your activity is the right activity.
UPDATE(Thanks to Santiago Gil):
It's necessary to call finish() on the onboarding activity.
I used toasts and found that the app was returning to the launcher activity, but it was not going through the onCreate so it appeared blank, I now call finish() on the onboarding activity and so far it's working fine. Thanks

What exactly does moveTaskToBack() do?

From the documentation:
public boolean moveTaskToBack (boolean nonRoot)
Move the task containing this activity to the back of the activity stack. The activity's order within the task is unchanged.
What exactly does "Move the task containing this activity to the back of the activity stack" mean? I know that each task is a stack of activities, but according the the above sentence, it seems there is also a global stack of tasks as well?
When I try this method out, the current activity moves to the background, and the behaviour seems very much like when clicking the Home button (e.g. the activity is not destroyed and can be resumed later). Is there any difference between calling this function and pressing the Home button?
There is not a "global stack of tasks." There's a global stack of activities, which can be from one app or from multiple. Let's say you have an app where you can click a link, bringing you to your browser. If the browser then calls a moveTaskToBack() method, then the original app activity will open, with the previous activities on the backstack still in place.
Now imagine instead of calling the moveTaskToBack() method, the user presses the Home button. Now, pressing Back on your phone will not take you back to the original app. You'll just stay on the home screen.

How do I determine if an activity is being launched from a back navigation versus being launched by the user in Android?

The home screen of my app needs to have different behavior when the app is being launched from outside the app versus when a back navigation occurs. For example, imagine an Android Twitter client which on launch tries to get your updated feed but when you click on an item to hit its detail page and hit back, the screen doesn't reload new feeds but is instead where you left from.
So far I've tried looking at the calling package property but this hasn't helped since it seems to be null both when app is launched by the user for the first time or when I hit back from a detail page.
When user launches it first time, onCreate() then onresume() will be called for sure. When navigating back and getting your activity from backstack, just onResume will be called.
Also if you activity has singleTop set, then you can look into onNewIntent() which will be called when you navigate back to your activity from other screens.
So, the solution I can suggest you is to set singleTop for your activity. Then while navigating back using onNewIntent(). For 1st time launch onCreate() will be called.
Sorry if I could not understand your question well..

Activity not closing properly

I am calling one activity on click of status bar notification which is having a Complete button. on click of btn. i have folllowing code -
public void completeTask(){
taskDBAdapter.deleteReminder(rowId);
taskDBAdapter.close();
Intent intent = new Intent(this, TaskManagerActivity.class);
startActivity(intent);
finish();
}
whhen i click complete btn new activity (TaskManagerActivity) gets opened properly.But if i reopen my application it still tries to open this activity and not my default landing activity. Any help on this.??
EDIT -
I have tried repositioning my finish() statement . Still its not working.
EDIT 1.1 -
Ok I will provide some details here. Assume my app has two activities
Main Activity
Notification Activity
My app create some notification to display on Status bar. So as soon as i click on status bar Notification actvty will open. Now there is a button called Complete on click of which the code given will fire and main activity (in the code TaskManagerActivity.class) will open. But after I press back button in my app and again reopen it , it opens the notification activity when it should have fired the main activity (as it is launching activity).
Thanks,
Ray
That's the default way android functions. If you press the home button and then open your app again, it will restore the apps previous state (unless it has killed the apps processes and activities due to memory constraints). So you are not actually restarting your app but only restoring it.
If you wanna quit the app, then press the back button. Now when you re-open the app, the original activity will be launched.
Do not modify this behavior. It is the default system behavior and users expect it to work this way. Your app is fine :-)
- First of all the behavior which you are experiencing is the way Android is made to function, moreover when a user gets a call while this app is open, after finishing the app he will definitely want to get back to the state where he left the application.
Still if you want it that way, here it is.....
- Make sure your application has only single instance of it running, by using android:launchMode="singleTask", or android:launchMode="singleInstance"
- Then finish() your Activity at onPause().
#Override
void onPause()
{
super.onPause();
finish();
}

Categories

Resources