How to make an app close itself completely (and cleanly) - android

I want to add "exit" functionality.
Display.getInstance().exitApplication(); almost works, but leaves
behind a "ghost" window in the android open applications view.

Why don't you finish all Activities and Services, and set every other reference to null.
That would leave you with just the Application Singleton alive if you have one.

Every Application on android OS, is like a User in Linux OS, and every application runs in a process which has a Process ID assigned to it which application does not know, but we if we kill the process and entire application will stop.
Here is how you can do.
android.os.Process.killProcess(android.os.Process.myPid());
but this would restart your first activity of your application, as android will try to recover the Application, you can avoid that by going to home screen like this
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();

Display.exitAppliction is the only way to exit an app but it won't be removed from the task switcher. Thanks to the comment from #david-medenjak I was able to find this:
android:noHistory="true"
To use that just use:
android.xactivity=android:noHistory="true"
In your build hints and the app won't be in history at all. But as people said this is a pretty bad practice...
FYI in iOS if you include an exit button your app will be rejected by review unless its a "logoff" button. Typical mobile apps never really exit and are only supposed to exit for security purposes.
The OS will kill your app if it needs the resources. Notice that this is important since mobile apps are stopped/restarted often e.g. incoming phone call.

Related

Launch an app using intent and continue from where user has left it

I developing an android app in which I want to launch an third party app and i managed to do it using Intents
Intent i=getPackageManager().getLaunchIntentForPackage("Your package name");
startActivity(i);
and it works properly,but I want to launch an app not as a new task ,it should be launched and resumed on the state where the user has left it.I have tried using set flags method but it didn't worked!!
Any solution,Thanks in advance..
Either the app can save its internal state on your request, and launch with state restoration. You need to find related documentation.
Otherwise, I fear it is practically impossible.
Saving the state from outside involve saving the whole process including OS resources in use. And relaunch with restoration means exact process restoration, including OS resources, which can be pretty complicated and almost impossible.

Start Android application without launcher

How to start an application that has no launcher activity?
Story behind the problem:
I have an application that is basically a BroadcastReceiver that waits for a couple system intents like BOOT_COMPLETED. The problem is that as my application has no Activity, it doesn't get started and so it receives no intent.
Android 3.1 release notes mention that intent options can be overridden to start up applications but I assume it requires another active application to do so.
P.S. Write all the ways you know. ADB commands as well.
First piece of advice would be to make a very simple "Welcome to my App" Activity that could be run. Use it to show a splash screen, some advertising, or be a settings screen. That gets you around the "no Activity" problem.
As far as I know, you cannot have anything hooking into BOOT_COMPLETED until and Activity in your application has been run. So you need to have an Activity of some sort.

android exit application completely and go to main applications screen

i know that this question is asked many times, but really i can't understand the answer, i want to set button, when user click it i want to exit the application (also the carbage collector should remove the objects), and after exiting i want to go to the screen where the user found the application icon on mobile.
for exit i don't know what to do
for going to the screen where to find the application icon i tried like this
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
but doesn't work
use this method, this will make your application to go in backgroud and will open home/app_menu screen
moveTaskToBack(true); // method available in activity
reference link
When you start that activity, it will effectively stop your application's foreground activities. Android is designed to NOT shutdown the app so that it may be restarted faster.
However, if you really really want to purge the app from memory, then what you want to do is kill exit() on the Runtime singleton : http://developer.android.com/reference/java/lang/Runtime.html#exit(int)

How can i close my app which is still running in background after closing

My application is running normally and when I move on some new activity I used the code as
Intent start=new Intent(current activity.this,new activity.class);
startActivityForResult(start, 0);
finish();
and then when I have to move back to the earlier activity I used the following code
Intent start = new Intent(current activity.this,earlier activity.class);
startActivity(start);
finishActivity(0);
I think in this way the stack of the activities may be clear. And on click of the button or the phone back button I used the following code
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(startMain);
But my application is still running in the background. I also used the android:noHistory="true" app is killed from the background but message appered that app close forcefully. Simple finish(); moves to the last activity.
I also seen the link Quitting an application - is that frowned upon? but I am new so did't get much. Please suggest me some way how can I deal with this.
I think in this way the stack of the activities may be clear
You are welcome to your opinion. startActivityForResult() is not designed for how you are using it.
And on click of the button or the phone back button I used the following code
I would fire anyone who worked for me who did this.
Please follow the Android design guidelines for proper handling of the BACK button -- while I do not agree with everything in there, it is much better than what you are doing. In the eyes of the user, BACK means back.
But my application is still running in the background
So long as you have not leaked any threads or services, your process will be running, but it will be consuming no significant CPU time. Android will terminate the process when it needs the RAM. This way, if the user elects to return to your application, or something else in your application needs to run (e.g., AlarmManager event, broadcast Intent), Android does not have to waste CPU time and battery life to fork another process.
Please suggest me some way how can I deal with this.
Just leave the process alone, and nobody will get hurt.

How to implement an app locker in android?

I am not satisfied with any of the app locker programs for Android that I have found and would like to create my own, but I am having trouble figuring out how to implement the actual lock. How does one go about implementing an app locker for Android?
There are two issues:
Detecting an Intent, usually from the launcher calling startActivity() or from an ad launching the market app (I'm not talking about broadcast intents -- I need to handle explicit Activity Intents).
The permissions for the locker apps I have seen all have "read system log files" and/or "retrieve running applications" which suggests they might use polling to look for app launches. I think I could do this, but I would like to find a better solution if possible.
Preventing the locked app from processing the Intent. I would prefer not to allow the locked app to run at all until the passcode is entered since it is impossible to tell what it might do when it starts up.
When running my current app locker, logcat shows "ActivityManager: Starting activity" first with the Intent for the locked app, then again with the Intent for the app locker, with nothing in between (separated by 10-20ms). If I enter the correct passcode then I see "ActivityManager: moveTaskToBack".
I have experimented with a test app which demonstrates that, using my current app locker, none of the Activity callbacks are invoked if the app is locked and I do not enter the correct passcode. So it appears to be preventing the app from starting (but I don't know how).
I could use ActivityManager.killBackgroundProcesses() to stop an app, but I have no guarantee that the app hasn't already started running by the time it gets "killed".
I might be able to use PackageManager.setApplicationEnabledSetting() to prevent an app from being instantiated, but I don't think that will take care of apps that are already running (which shouldn't be allowed to process new Intents or come to the foreground). I might be able to use killBackgroundProcesses() to stop all running locked processes so they would have to be re-instantiated. But I don't like this approach because other apps could mess with the Enabled settings and defeat the lock.
Optimally, the system would send every Intent to my app for inspection and allow me to stop or let it pass through, but I'm pretty sure that's not possible (at least, not for explicit intents to start activities from a launcher).
Does anyone know how app locker apps are implemented, or have any bright ideas on how it could be done?
I would look into the lifecycle. Once the app in question begins to load, some activity from that package will be added to the forefront activity.
A scan of the changes in forefront activities might do the trick.
http://developer.android.com/reference/android/app/Activity.html#ProcessLifecycle

Categories

Resources