0

We have a live video streaming app with a lot going on. A user presses the home button. I want the app to be removed from memory. When the app is selected again we have a brand new load. There are a lot of processes going on and we don't want to have to manually manage all the connections, streams, etc. This is how our iPhone version of the app works.

I've read this: Is quitting an application frowned upon?

I don't really care about Androids design patterns here either way. However if someone has an elegant, simple way that all my activities will be removed from the stack when the home button is pressed, and then when the app is reloaded it starts with a fresh main activity, that would be great. Also, I can't seem to ever debug when the home key is pressed in onKeyDown. Its simply not registering. (keyCode == KeyEvent.KEYCODE_HOME) is my check. It picks up back buttons, etc.

Any thoughts?

Community
  • 1
  • 1
spentak
  • 4,627
  • 15
  • 62
  • 90
  • 1
    possible duplicate of [Kill all activities when HOME key is pressed android.](http://stackoverflow.com/questions/5308088/kill-all-activities-when-home-key-is-pressed-android) – Squonk Jun 15 '11 at 17:56
  • You can iterate through all of your activities, and invoke finish() on each one. Unless you also have a service running?? – IgorGanapolsky Sep 12 '12 at 17:30
  • This question accepted a wrong answer. – Glenn Maynard Mar 11 '16 at 04:49

5 Answers5

3

You can call system.exit(0); but I would still suggest to follow the Android guidelines, and clean everything (as should be) on onPause() or similar method.

MByD
  • 135,866
  • 28
  • 264
  • 277
1

Could you just override the onPause method and use the finish() function?

Matt
  • 5,461
  • 7
  • 36
  • 43
1
int p = android.os.Process.myPid();
android.os.Process.killProcess(p);
Philip Sheard
  • 5,789
  • 5
  • 27
  • 42
  • How about the bit that begins "I don't really care about Androids design patterns"? Did you read that? – Philip Sheard Sep 12 '12 at 21:08
  • This solution does not guarantee the app being removed from memory in any way, shape or form! Do you even understand what 'remove from memory' means? If you do, then you'd know that you cannot force Android to 'remove' anything unless you reboot your phone or uninstall your app. So unless you know how to root your phone and modify system files, you can forget about forcing Android to truly kill your app with such a stupid strategy. Why do you think task killers don't work? In this situation, follow Android's design guidelines about finishing activities and unbinding/stopping services. – IgorGanapolsky Sep 15 '12 at 18:32
  • Marking down my answer does nothing to strengthen your case. It is just the sign of someone who has lost an argument. – Philip Sheard Sep 16 '12 at 10:03
  • You don't have to be so defensive – IgorGanapolsky Sep 16 '12 at 13:43
  • You are clearly an expert on operating systems, so tell me this - what does operating system do when a program abends? – Philip Sheard Sep 16 '12 at 17:29
  • When you understand that Android is a layer on top of the Linux kernel, then we can talk. If you keep insisting that Android manages processes just like Linux, then you are mistaken. Using android.os.Process.killProcess(mPid) or system.exit(0) may reset your application stack to its original state, and hence not quit the app. I have seen this happen, especially if you have foreground services started from your application. – IgorGanapolsky Sep 28 '12 at 18:36
  • 1
    Nothing like StackOverflow to have to wade through a page of bickering to find any info. – Glenn Maynard Mar 11 '16 at 04:48
0

If you want to clear all of your activities from the stack, you can broadcast an intent from the activity which initiates the quit like this:

Intent broadcastIntent = new Intent();
broadcastIntent.setAction("CLEAR_STACK");
sendBroadcast(broadcastIntent);

And in every one of your activities that you want to be cleared off the stack you can create a BroadcastReceiver with an inner class:

class ActivitiesBroadcastReceiver extends BroadcastReceiver {
  @Override
  public void onReceive(Context context, Intent intent) {
    finish();
  }
}

And register your BroadcastReceiver in the onCreate() method:

IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("CLEAR_STACK");
BroadcastReceiver r;
r = new ActivitiesBroadcastReceiver();
registerReceiver(r, intentFilter);
IgorGanapolsky
  • 26,189
  • 23
  • 116
  • 147
0

you can do than on button click. Define any static method like exit() and define

android.os.Process.killProcess(android.os.Process.myPid()); in exit method

in the main or first activity.

Then call this method on button click.

Ankit Dhingra
  • 125
  • 1
  • 5