What happens when android app is "killed" - android

I have an app which has real time push notifications enabled. So my query is whenever any user tries to kill my app using "Advance Task Killer", my app goes into undefined state.
Undefined State: What i mean is my app doesn't gets completely terminated. The screen has data in a inconsistent state.
So is there way where i can take a user to login screen back whenever an app is killed. So that user wont see any undefined state.
Also want to know what happens to my app states after it gets killed so that i can fix the issue.
Help Appreciated.

It depends on the version of the platform. Prior to 2.2, third party applications like ATK could use an API that did the same thing as the "Force Stop" button in manage apps -- this kills all app processes, removes all tasks/activities, unregisters all alarms, removes all notifications, stops all services, etc. Basically make the application not running the same as if it was first installed, except its persistent data is still intact.
Needless to say, this tends to cause misbehavior of applications, so as of 2.2 other applications like ATK can no longer do this to your app. The API they were using is now only able to do the same thing that the out of memory killer does -- kill the application processes but only if they are in the background in a killable state. It can do no more than the normal out of memory killer, so as of 2.2 if your application is misbehaving due to an app like ATK being used on it then this is exposing an actual bug in the app that users will encounter through the normal use of their device.

Related

What exactly does "removing an app" from the multitasking list do?

When I remove the app from the multitask list, it doesn't seem to kill the app's processes + services, but when I reopen the app, it starts from the "beginning" such as the homepage. What exactly does removing the app from the multitask list do exactly?
Generally removing an app from your multitask list is the same as quitting an active app (by hitting the back button continuously). The app stops functioning and the background processes linked to the app are killed. But, as you mentioned, it doesn't always happen because there are certain apps that require certain processes to run- for instance, closing an email app still lets it check for email and perform certain functions.
Force stopping your app is what actually kills all background processes as well, however again there are certain apps that still avoid this- this could be because they have some sort of link to the maker of the OS that allows those processes to run. For example, the Gmail app and Android OS both belong to Google, so they probably can decide how their app runs regardless of what you do as an end user.

stopping the user from forcing stop my app

About Android (6.0 to the last version)
I'm developing an app and we want that the user, once he accepts all the terms, don't be able to kill the process or force stop the app. Honestly, I'm completely lost right now, because on the last versions of android, and specially some brands like Xiaomi, we are having a lot of trouble with it, and we don't know how to act right now.
In the case that it could not be possible, could at least get an alert whenever the user is killing the app?
Thanks!!
It is not possible to prevent the user from killing an app. Android is a unique system where the app has no direct control over its lifecycle but the system has. The system can (and will, when required) kill the app or any of its processes at its own will. To make your app aware of these changes, the android framework provides for various callbacks such as onPause, onStop and onDestroy which are called in succession when the user kills the app.
Side Note : There is no guarantee that onDestroy() will be completely executed when the app is killed. Do not place essential code there.
Of course, you can block or try to prevent the user from closing your app by overriding the back, home and recent buttons but it is highly recommended not to do so. Even if you do so successfully, the user has other means to close your app such as rebooting their phone.
So what to do?
You are looking for a kiosk mode app. Kiosk mode is used for single purpose phones such as at a restaurant or for a cab driver. Kiosk mode apps lock down the user to only a specific app (or a specific set of apps).
For normal apps, it is not possible to prevent the user from force closing your app. You can only get alerts by checking for lifecycle changes as described above. Moreover, it is not at all recommended to change the natural behavior of the hardware buttons on android. The user can still find a way to close your app. If your app is doing something really essential which should proceed in the background, consider using a service for that instead. Also, the user can uninstall your app at anytime if they find your app being too intrusive and you won't be able to do anything in that scenario.
Tl;dr: Use kiosk mode to prevent the user from exiting the app. This will only allow the user to access your app(s) in their device.
Usually you cannot! Even if you try to disable some buttons, user can always stop app or restart device. In addition at times, the OS will stop the App. Your responsibility as a programmer is to program around this, and give the user the feel that it never stopped. If you are doing background monitoring, you will need to use service. Users will still be able to stop service. Having said that, you can set your app as a Device Administration app, see here, which may disallow stopping, but unless you are distributing internally to a company, noone will install.

Check if app is running and kill

I was wondering if I can kill app from other app. I mean I want to create application which when I start this application I will check status other application and if app, which name is for example "Startex" is running then I kill "Startex" app and run my second application.
No you can not. Only Android can kill application if it needs memory. In order to do this you should know the PID of the application you want to kill and invoke a syscall at kernel level
If both apps are designed by you then you can have shared user id in both the Apps then you can kill another app here is the way.
This can't be, or at least should never be done. Android itself is supposed to control when an app is no longer required, usually based on needing to free up some memory for another app that may be opened. Google frown upon developers killing the app themselves without letting Android handle it, and likely would never receive any promo place on the play store from Google.

Why is my process always showing in the DDMS processes list?

Using DDMS, I see 3 processes active in my Android: Viber, logitech harmony and a process with my package name.
The thing is that I never created any process.
Why have Android opened a process on my behalf? For what purpose?
Also, why can't I see all the other apps' processes, like gmail, maps, etc. even when they are found in the foreground?
Some thoughts: I do listen to folder changes using a FileObserver. Perhaps that's the thing? Perhaps it's something related to running the app in a development environment?
Why have Android opened a process on my behalf? For what purpose?
Because you ran your application.
Also, why can't I see all the other apps' processes, like gmail, maps, etc. even when they are found in the foreground?
Because they are not compiled in debug mode. On a production device, only debug builds (or apps with android:debuggable set to true in the manifest) will appear in DDMS.
Each app is launched in a separate process. Hence your app is being shown as a process. Perhaps other apps are not active on your phone hence they are not being shown.
Unless the phone is starved for memory, Android keeps processes alive just in case. When you close an activity, the process stays.
That, or you might have a service running. When a service is running in your app, Android treats the process with even more respect, meaning it gets shut down only after all the do-nothing processes are shut down.

Is it fine if I see my application from Settings App in the emulator has Force Close option enabled even tough I have come out of my application?

I have written a simple database program in android. It runs fine, there is no force close error. But I checked from my application from Settings App I see the Force Close option enabled, which implies that my application is still running in the background, even though I have completely came out from my application to the home screen by pressing back key. And moreover I am not using any services, alarm or broadcast things.
Can some one please guide me what may be the probable reason?. Or is it okay? Or will it crash if I put it on device?
Can some one please guide me what may be the probable reason?. Or is it okay? Or will it crash if I put it on device?
Your application is alive until Android OS needs more memory and destroys it. What I have understood does Android start destroying activities before killing the whole application. This means that your application can be alive even if you have finished your activities.
Do not worry about this; Android OS is handling this extremely well.

Categories

Resources