Why does a task still exist after pressing the back button? - android

The Android docs say
If the user continues to press Back, then each activity in the stack is popped off to reveal the previous one, until the user returns to the Home screen (or to whichever activity was running when the task began). When all activities are removed from the stack, the task no longer exists.
Well I tried this on a galaxy s4 with android 4.3, but afterwards when I hold the home button to show all tasks the task is still there (tried with settings app, help app, internet app, my own app). In what way does it not exist?
Edit:
I'm reading up on tasks to understand the Android system. I have no specific app development problem.

Related

How to restore a specific activity that is not the launcher activity and instantly navigate to it after the system has killed the app

I have multiple activities in my app and a specific problem in which after my app is killed by the system, upon reopening the app (icon or from the overview) it starts loading my launcher activity with the splash screen instead of loading the last activity that the app was using.
My launcher activity is essentially a form of a login screen which should be used only the first time the app is opened, but instead it is reopened every time the app is killed by the system and app reopened by the user.
I am inexperienced and do not know if this is the default behavior of Android or a bug, I have searched through stack overflow but could not find a conclusive answer.
Coincidentally this well known long standing Android bug has appeared. (https://issuetracker.google.com/issues/36941942)
I do not know if the two problems might coincide, and I cannot be sure when the longstanding bug has first appeared because previously I was testing through Instant Run which does not result in the bug, but am now forced to test through play because it does result in the bug.
Thanks.
I am inexperienced and do not know if this is the default behavior of Android or a bug
This is the default behavior of Android, if there is no current task associated with your app. That includes:
User swiping the task off of the overview screen
You terminating the process and task from inside Android Studio
All the time if the user has been out of the app for 30+ minutes
User using Force Stop on your app in Settings
And probably some other scenarios that I'm not thinking of right now
My launcher activity is essentially a form of a login screen which should be used only the first time the app is opened
I recommend that your launcher <intent-filter> be on your primary activity in the app. If that activity determines that the user has not logged in, it can do something to authenticate the user (display a fragment, start an activity, etc.).

Android studio App home button and re open

Android studio home button problem.
I have so many activities, when I press home buttom in any activities.
Then if I restart my app.
it started from splash activity(logo activity).
However, I want to start this from activity where I pressed home button.
Can anybody figure this out?
By default, Android handles this behavior. When you press the home button, the app should go to background and at the next time when you open it, it should start from where you left off. But, Android's memory management is designed to automatically terminate minimized apps that have not been accessed in a while when memory is needed for newly launched apps.
If there is enough memory available and still your app gets terminated, that means you are not using the API's correctly. Please read this [article] to know how to handle onPause() and onResume() to achieve this behavior.

Android: App screenshot preview in task list is blank?

In Android when you hold down the home button you can bring up a task list of your recent apps. And each "card" in the task list should have a preview of the app. However, this time I opened the task list and the previews were blank (except for the app title).
I had the phone Settings in the task list and every time I would tap on the task and try to open it in would close immediately.
I also had this app I was developing and when I tapped on the task to open the app my RecyclerView wasn't showing anything, when it should be.
I also was playing AngryBirds 2, so was it that Android had silently killed those apps for memory? If so, does onCreate() of the killed apps get called again when this happens?
Edit: I just noticed if you close an app too quickly when it's transitioning to an Activity you can get a blank preview.
Edit 2: So to answer my question, after referencing the Activity lifecycle diagram this is what I found. When another application needs memory, the app's process will be killed and when the user navigates back to the app, onCreate() will be called again. However, that doesn't explain why I couldn't reopen the Settings. And I have no idea how to reproduce this weird behavior!
Ok after some debugging, I found out that Android did indeed kill my app because it needed memory. When you resume to your app however, onCreate will be called with a savedInstanceState that is NOT null. I had some logic that required savedInstanceState to be null and therefore my RecyclerView wasn't populated.
To recreate this scenario of Android killing my app I had to open a ton of apps lol. Is there way I can reproduce Android killing my app without opening all those apps? Hm...

Returning to calling client with Back button when normally Back is disabled

My app is designed to run as a single instance and the Back button does not allow you to exit the app and return to the Start screen because it is used internally to navigate a hierarchy of screens where each screen can be an activity.
However, an external app can launch one of the app's internal activities. When the user is done with whatever the activity is designed for, the user's intuitive action is to hit the Back button to return back to the calling client. But because I prevent the Back button from exiting, the user cannot return.
I can add code to override this when the code detects that the activity is being launched by a client. The problem however is that if the app closes to return, the user might return to the app from where they left off. But since I closed the app to return to the calling client, the user cannot return back to the app as it was last opened. My app needs to remain as a single instance, so the activity that gets launched cannot be created more than once. Any suggestions on how to return back to the calling client but also keep the app running if it was running when the calling client used one of its activities?
In general, you can programmatically control the back navigation trail. Take a look at
TaskStackBuilder and the documentation for handling notifications Responding to Notifications. It seems that what you're trying to do is control the so-called "back stack". If you use TaskStackBuilder, the behavior will match the platform version you're on.
In pre 3.0 platforms, the Back button went all the way through the back stack to the first task the user did since the phone was turned on. Post 3.0, back does not traverse task boundaries; to get to other tasks, the user clicks the "Recent Items" icon. There's also the "up" icon in an app to navigate to the "beginning" of a task within an app. TaskStackBuilder will "do the right thing" for all versions.
In the current platform version, not allowing Back to exit your app is OK, because Back should only go to the first Activity in the current task. In versions previous to 3.0, not allowing Back to exit the app is more problematic, and I personally wouldn't do it that way, but it's up to you.
What happens when the user clicks Back after an Intent starts your app should be clear from the documentation I've cited. Basically, what you want to do is go back to the previous task.

Install via OTA, app starts from splash when minimize and re-enter

For simply, I write an app with Splash, Home, Child1 and Child2 with following flow: Splash -> Home -> Child1 -> Child2. And we can back from Child2 to Child1, from Child1 to Home, and back from Home to exit app.
It sounds simple. I can navigate between these screens correctly.
Problem when I install app via OTA. After enter Splash -> Home -> Child1 -> Child2. Staying Child2, I press HOME button to minimize app, and re-enter app by choose app icon. But when re-entering app, it doesn't resume at Child2 but start from Splash -> Home. I cannot understand what happens here. It should resume at Child2 instead.
More surprised thing is, at Home screen, when I press BACK button to exist app, now Child2 is resumed (o_O). And from Child2, now I can back to Child1, and to Home, and back again to exist app.
All these issues happen when I install app via OTA but app works normally when I install by cable.
Can anyone tell me what happens in my situation? Any help is appreciated
Many thanks.
P/S: I get this bug when testing on Nexus 1 (Android 2.2) and Nexus S(Android 2.3.x)
Edit: even CNN app for Android still have this issue... Is it belong to OS?
This has to do with task affinity I guess. And it's not just the CNN app, all apps behave like that because some developers (like me!) don't understand how the task concept works. I won't lie to you, even I don't understand this all the time.
I believe this happens because, when you launch your app from the Market, your app becomes part of the Market task. Therefore, you have an instance that "belongs" to the Market task. You can navigate normally from it. All the problems happen when you press Home because...
If you start the activity from the Launcher, you start a new task for the app, giving you 2 "instances" of the app, one belonging to the previous Market task, the other belonging to your own app (where you are the root of your own task).
On this new task, you can navigate back, and eventually you will reach the previous task, which has another instance of your app. You will reach the point where you left off (considering regular launch mode for the activities). Sometimes, this is odd because we normally think that one app has only one instance, when it's not how it works, nor it's how it should work. I loosely compare that with launching twice an app in Windows having two Windows open. In Android, this analogy is similar (at least to me).
This is all fine when you start your app from another app, when the task concept feels natural.
However, in this case it becomes weird because when we launch the activity from the Market, we expect the instance to be the same that will be in the Launcher (when launched from there). We naturally don't expect that the Market, used to download the app, will own an app that we installed, and that we (obviously) want to use separately from the Market app (I don't think a Market should own a task when using the "Open" button).
To solve that, I believe you could fiddle with allowTaskReparenting. However, in case it's really that which is causing this issue, I don't recommend it because this could break other legitimate uses for the task system.
Finally, this is how I believe tasks work, and that's my guess to where this problem comes from. I could be wrong, so please forgive me if this answer becomes useless later (if you find another cause that answers this).
Good luck!
Looking at your comment you kill your activities as soon as you start a new one..
E.g. if you do:
startActivity(intent);
finish(); // THIS WILL KILL THE ACTIVITY -> MAKING IT UNREACHABLE
The activity that launches the new activity is being killed and cannot be reached using the back button.
Remove the
finish(); // REMOVE THIS TO KEEP IT ON THE ACTIVITY STACK
after you call startActivity(intent), and all your activities should stay in memory. Hence, they will be reachable when you go back. NOTE: for the Splash, you should indeed call finish(), since you don't want to go back to the Splash activity after a succesfull launch!

Categories

Resources