Exit Button on App? App Still Running? - android

I have a question about the exit button. I have read several posts on here about use of an exit button/back button. Also the blog: http://blog.radioactiveyak.com/2010/05/when-to-include-exit-button-in-android.html, which clears discourages the use of an exit button.
Here is my question. I have an app that is very small; however it pulls data from the webservice/MySql database online. It is supposed to only pull data on first open. Or, if the user selects update data from a menu. I do not have an exit button in the app, However I thought that if the user would back completely out of the app, this would be the same as an EXIT.
After backing out of the app, I can still see the app in Setting>Applications>Running Services. It says "1 process and 1 service". In Manage Applicaions, it says that the app has been running for 36 hours.
Is this okay? I do not want users to think my app is using their battery.
On a separate note, I do not see an additional updating (pull from webservice) after backing out. But if I install the app on my galaxy Tablet 10.1 running Android 3.1, I do see an occasional update from the webservice.
Anyone have some advice for me?

Android won't stop an application when the user presses "back". The application will stay on memory until Android needs the memory for another application. Any thread that was running when the user presses "back" will continue to run. Traditionally, you're supposed to stop all those processes on the onPause() method of your Activity (additionally, store all preferences and other cleanup.)
Also, if you've started a Service (as stated), then it will continue to run until you tell it to stop. On rare occasions, Android will kill a Service to free up resources, but for the most part you have to kill it.

As you mentioned it is showing 1 service running. So you need to stop this service and release the resource if any you have used in the service as android will not do it for you.

If you're only downloading the information with the webservice on startup and then manually, why not just stop the service, once its downloaded the data and then start it again when the user has requested a manual refresh?
Android rarely kills a service in Android, however will (whenever it wants) stop your application from running in memory. BUT with how the Android handles activities, your app will likely be started again from the last activity window it was in.

After backing out of the app, I can still see the app in Setting>Applications>Running Services. It says "1 process and 1 service".
This means you started a service and never stopped it. For an operation like the one you describe, perhaps you should be using an IntentService, which automatically shuts down when the work is complete.
Is this okay?
It is certainly not ideal. Some users get very irritated with apps that behave like yours, using task killers or the Manage Services screen in Settings to force-stop your app.

First create a button and place this code onClick event
System.runFinalizersOnExit(true);
System.exit(0);

Related

Differentiate between Android killing the app and user swiping it off on the recent apps list

I am working on a project, where while being on a specific Activity we show a local sticky notification. That should also be the case when the app is minimized. What I have to accomplish is to remove the local notification whenever the app is killed (by Android, because of memory lack or by the user, with a swipe from the recent apps list).
Usually onDestroy would be called whenever Android takes the Activity to open some space. That is fine in one of the cases, however swiping an app from the recent app lists doesn't call the onDestroy and the sticky notification stays.
What I did is, I implemented an empty Service which would force the onDestroy when the app is killed (both swipe and system kill) so I can get my notification removed.
However, what I would like to do is to differentiate between the swipes and system kill.
Is this even possible?
In general, if Android wants to kill your application because it has been in the background for too long (or because it wants to reclaim resources), Android will just simply kill the OS process hosting your app. It will not call finish() or onDestroy() on any Activity or Service components. The behaviour of "swipe from recent tasks list" has changed over time and is different in different Android versions. Someone should write a book about that :-(
You can check for when the user swipe-closes the app by adding a service to your app, and implementing the onTaskRemoved method: https://stackoverflow.com/a/26882533/2441655
This is a comment I found in reddit that seems to me really interesting:
Swiping an app away will effectively "kill" most apps. You can test
this out using ADB if you have the SDK installed. Swipe everything out
of your recents list, then launch the browser.
Use ADB to run 'ps' on the device and verify that the com.google.android.browser process is
running. Go to the home screen, it's still running. Launch some other
apps, and the com.google.android.browser process is still there.
Swipe it out of the recents list, however, and the process is gone. You can
create a test app to further verify, and log the onDestroy() call in
your Activity. It's not called when you back or home out of the app,
or when you launch other apps. It does get called when you swipe the
app out of the recents list though. I do agree that the recent apps
list isn't really "multitasking".
The apps in the list aren't necessarily even running, the processes could have been killed by the
memory manager long before you try to re-open it. However, you can't
argue that the only purpose is to jump quickly to other apps when the
swiping makes the actual process go away.
This is another good answer about what happen when you swipe an app out of the recent apps list. But the part that I liked most was:
Actually, removing an entry in recent tasks will kill any background
processes that exist for the process. It won't directly causes
services to stop, however there is an API for them to find out the
task was removed to decide if they want this to mean they should stop.
This is so that removing say the recent task of an e-mail app won't
cause it to stop checking for e-mail.
If you really want to completely stop an app, you can long press on
recent tasks to go to app info, and hit force stop there. For stop is
a complete kill of the app -- all processes are killed, all services
stopped, all notifications removed, all alarms removed, etc. The app
is not allowed to launch again until explicitly requested.
By Swiping from recent task list removes only from recent tasks ..
It was also called onDestroy before android 5.0 .
Might be you are having issue above api level 20 devices.
System kill normally can not be executed in normal android activity lifecycle.
It just finishes the activity on back press event.
when swiping app to left if any Thread still run in your app Interrupted but service not stopped, when you kill handy app Thread and services are stopped.
the behavior is similar to but not exactly the same as closing an app -- in general (for apps that don't define explicit back button handling) it's the same thing as hitting back enough times from within an application that you exit out of it.check out this link discussion it has some good input on the subject
First, let's get one thing clear: Android MAY NOT CALL onDestroy(). Referring to the Activity Page, from Honeycomb onward, onPause() and onStop() are guaranteed to be called before an app is killed.
Be aware that these semantics will change slightly between applications targeting platforms starting with HONEYCOMB vs. those targeting prior platforms. Starting with Honeycomb, an application is not in the killable state until its onStop() has returned. This impacts when onSaveInstanceState(Bundle) may be called (it may be safely called after onPause() and allows and application to safely wait until onStop() to save persistent state.
So after (hopefully) clearing the air on the Android lifecycle, I think you can achieve what you want by putting the notification removing code in onStop() instead. If you end up needing it back because the user actually DOES come back to the specific Actvitiy(IE not killed), you can bring it back in onRestart().

Resume Back Kivy Application even if minimized (not closed) to recent activity

is there a way to resume back a Kivy application to most recent activity even if minimized? i mean when i am in the application ( a compiled application as .apk) and i want to open wifi for example or check other apps, my application closes and i need to restart it again by pressing on it again...so if i am in the middle of an operation in my application (a calculation or an e-mail composition) and i want to check another application ,all my operation would be cancelled if i do so and of course it is a terrible user experience...
The documentation here describes how to enable pause mode, such that your app will be resumed rather than restarted if you leave then open it again.
The main point is to add an on_pause method to your App class, and have it return True.
Note that you are never guaranteed to be able to unpause again (Android itself may kill paused apps if their memory is needed etc.), so you should also use this method to carry out any state saving you need. However, it's unlikely to be a problem for short task switches, and may rarely or never come up in newer devices with more resources.

How to prevent a user from stopping an Android service?

I created an Android app and need to make it difficult for users to stop the main service that the app spawns during its startup process. This is for a rooted Jelly Bean 4.1.2 device. Here are some steps I've taken so far:
Installed as System App
Uses the Device Admin APIs
android:allowClearUserData="false" is included in the AndroidManifest.
The steps I've taken so far takes care of most normal ways a user would stop/disable an app/process; however, when you check the running apps list in Settings -> Application manager -> Running, users can still hit the 'Stop' button on the long-running service that was started by the app (see picture below):
Is there any way to prevent users from stopping the service here? Or what's the best way to restart a service when a user hits this stop button? I tried putting some code in the service's onDisable() function, but that function does not seem to be called in this case.
Any help would be appreciated!
As explained above does not have this option unless it is executed as root, but you can create an AlarmManager when starting your service that runs from time to time, the system will run if the service is not running, it will be created again.
Is there any way to prevent users from stopping the service here?
Having your app be a device administrator probably blocks this. It definitely blocks the "Force Stop" option.
I tried putting some code in the service's onDisable() function, but that function does not seem to be called in this case.
Since there is no onDisable() on a Service, this is not surprising.
This is a security app for an enterprise, so its expected to be continuously running.
There is nothing intrinsic to "a security app for an enterprise" that would require it "to be continuously running".

Android retain AsyncTask state/progress

In the app I'm developing atm. I use asynctasks to upload videos to a website, as it stands now if the application process is killed (User returning to home screen using the back key), those asynctasks are lost. ideally I would want the uploads to carry on despite the application process being killed, but I don't think that is possible.
I wonder if there is a way to retain their progress somehow (Maybe support from the website API is necessary?), or if not at least save the details of the asynctask and restart it when the app is opened again.
Vimeos application seems to have been able to resume video uploads, even after having killed the application process, thats exactly what I'm hoping to achieve.
Appreciate any ideas and suggestions.
I think you may be using the wrong architecture.
Anything that needs to survive in between Activity transitions is more suited for a Service. A service runs in the background (possibly even after the app is closed) and lets you do long running things such as performing uploads.
To kill the app process but have the Service continue to run, you can assign the service to a separate Android process using android:process in the manifest.
http://developer.android.com/guide/topics/manifest/service-element.html#proc
See this thread too:
How to keep a service running in background even after user quits the app?

On Android how would I be able to execute some code for a few seconds after a user either quits or uses another app and then comes back?

Basically,
I need to for a queue collection to persist about for about 15 seconds after a user either shuts down the app or switches to another application and then, presumably, comes to the app.
I'm looking to either save the items in the queue or process in some other way the items during the 15 seconds that they persist.
Is this possible? Can anyone point me in the right direction?
This is my second android app and the first time I have to do this so any help would be appreciated.
You can use onResume() to run commands when they com back from another task.
As for running something X seconds after they quit the application, that would require to include a "service" in your application, which could be called by your task onStop()
Although it is possible for your activity to remain active in the background you should not depend on it, as different devices will be more or less agressive with resource reclamation.
Using a service is the only sure fire way to run something in the background while orhter applications run.
http://developer.android.com/reference/android/app/Activity.html
http://developer.android.com/guide/topics/fundamentals/services.html

Categories

Resources