10

My android application allows to launch other installed applications from this.This shows some allowed apps. If the user try to launch a disallowed application then show a message and go back to my activity (from where each application launch) using running tasks. My application act as a home launcher.So intent to this activity if the is a blocked application.For eg: It is possible to launch Camera from Gallery in Samsung device.If the camera is not an allowed one shows blocked message and exited to my acivity.But when relaunching Gallery the blocked message shows again beause the top activity(Camera activity) lied in the stack.

But the exiting of these blocked application did not work perfectly.

  1. Is it possible to get close/exit event of an application?

  2. How can i finish an application as whole(By finishing all its applications).

  3. How to launch an application without having any history of previous launch?

Thanks in Advance

Triode
  • 11,309
  • 2
  • 38
  • 48
Devu Soman
  • 2,246
  • 13
  • 36
  • 57

1 Answers1

14

Is it possible to get close/exit event of an application?

Yes it is possible inside your LauncherActivity You can override onDestroy this method will be called on application exit.

How can i finish an application as whole(By finishing all its applications)?

I believe you want to stop your all running activities here. This can be achieved in multiple ways.

android.os.Process.killProcess(android.os.Process.myPid());

or

Intent intent = new Intent(getApplicationContext(), YourHomeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

This will clear all the activities and will brings the user to the HomeActivity. While calling this you can add a flag in intent and using that value you can finish the HomeActivity also. Use finish() method to finish the activity.

How to launch an application without having any history of previous launch?

You can use the same above Solution to achieve this. The second one.

Hope this will help.

There is an onTerminate method in application class, but this cannot be used in production environment. See more about this here

Triode
  • 11,309
  • 2
  • 38
  • 48
  • While using onDestroy(),here i detect exiting of my application.I want to detect the shut down event other applications which launch from my application. – Devu Soman Apr 30 '13 at 12:07
  • 6
    Wait.... Override the onDestroy function of the LauncherActivity? What if the LauncherActivity navigates to another Activity? That will call the onDestroy as well. This seems disastrous to me. Can you please correct me if I am wrong? – Subby Apr 02 '14 at 14:25
  • it wont call onDesyroy until and unless you finish the activity or clear the activity stack. – Triode Apr 02 '14 at 16:38
  • what about onBackPressed in Launcher activity ? – Suchith Jul 15 '15 at 04:57
  • @Triode , is it also called when i am in some other activity, i press home button and i then remove its instance? Can we get onDestroyed() called in such case as well? – Shruti Dasgopal Jul 15 '15 at 05:34
  • @ShrutiDasgopal No, home button click does not clear the activity back stack, it actually pauses all the activities and all the activities will be resumed when you comes back to the application again. – Triode Jul 15 '15 at 05:41
  • 1
    Then in my case i won't be able to detect an exit of my application? – Shruti Dasgopal Jul 15 '15 at 05:53
  • When activity gets destroyed you will get a call to onDestroy and also if the app is killed by android then you will get a call to onSavedInstanceState as well. – Triode Jul 15 '15 at 08:57
  • This is far from ideal. Android should have a listener for this in the `Application` class. – André Fratelli Jun 23 '16 at 01:16
  • @AndréFratelli in fact there is one method onTerminate in the application calls, But the doc says this cannot be used in production environments. See here https://developer.android.com/reference/android/app/Application.html#onTerminate() – Triode Jun 24 '16 at 04:43
  • 1
    @Triode, I know. I've been looking for how to catch close events and solved it with a work around. Android's API has been very disappointing. – André Fratelli Jun 24 '16 at 04:52
  • @Triode `onDestroy()` can be called even in low resources. See [this answer](http://stackoverflow.com/a/4449988/1276636). So if I turn on the Developer option on Android device to "kill activity as soon as it leaves view", it will mess things up. – Sufian Jul 18 '16 at 12:30