-2

I have an app which I want to open, use, and then at the push of a button close. My question is do I need to keep singleton data of the application status in order to make this happen? I mean if I go with the solution of doing finish on resume() that means that I should need to keep a global data that each activity looks at to close out? This is a fine, albeit awkward way to close an app, but I'll go with it unless I hear another way soon.

Thanks.

PS please do not respond with android will decide when and what to do with your app. I'm sorry but I know my user does want to see this app again after they click Finish! And in the end that's what matters and they are not at all interested in what android needs to do with the app nor should they be.

Androider
  • 21,125
  • 36
  • 99
  • 158
  • 5
    possible duplicate of [Quitting an application - is that frowned upon?](http://stackoverflow.com/questions/2033914/quitting-an-application-is-that-frowned-upon) – CommonsWare Mar 29 '11 at 20:06
  • Well I don't think it was really answered effectively there. – Androider Mar 29 '11 at 20:07
  • Understood, but what I am trying to do is get an opinion on whether I need to keep singleton data to resolve this issue. – Androider Mar 29 '11 at 20:08
  • 1
    @Androider: it's not really clear what you want: do you want to destroy the app as in a regular desktop app or you want to hide it and make sure it remains open for next time? "I'm sorry but I know my user does want to see this app again after they click Finish!" is a very confusing sentence. – Aleadam Mar 29 '11 at 20:10
  • Your question is not so clear. can you detail a bit more? – MByD Mar 29 '11 at 20:12
  • Yes, it was partly answered in a number of posts, but I don't think those answers are what is needed. We need to get a consensus on what is a rational thing to do with this OS limitation. – Androider Mar 29 '11 at 20:12
  • Ok. I have an app with Activity A1, A2, A3, A4. If I am on say Activity A3, and I press Done/Finish. I don't want some other activity like A1 if it is the main activity to resume. I want the app closed out so that it will never show up again until the user relaunches it. Thats the situation, and I think it is somewhat common. – Androider Mar 29 '11 at 20:14
  • Its all about Intent. Does the user not have the right to issue the Intent. I want this thing closed and I don't want to see it again until I ask for it? – Androider Mar 29 '11 at 20:15
  • @Androider: It is not an "OS limitation", any more than it is an "OS limitation" that you cannot forcibly close a user's browser from your Web app. – CommonsWare Mar 29 '11 at 20:15
  • Doing finish() in just one of the activities is not enough. It only closes that one activity so another one can resume especially the main one. So really at more than one activity has to do finish and this is why the question about state, and now the need to manage it. – Androider Mar 29 '11 at 20:17
  • Yes, but you can close the browser. And its an app. What if you closed the browser and it just kept popping up. – Androider Mar 29 '11 at 20:18
  • Its all about the users right to end the usage of the app until they relaunch it. – Androider Mar 29 '11 at 20:21
  • BTW, not all android situation will necessarily be phones or have all the buttons of a phone. What about dedicated devices that use touch screens. Thats why this capability is important. – Androider Mar 29 '11 at 21:57

3 Answers3

2

Maybe you can use System.exit(0) if you want to kill your app.

As came up from comments, you need to do it from the main activity. My way (though there might be a cleaner way) is to add a static handler in the main activity:

static Handler handler = new Handler() {
   @Override
   public void handleMessage(Message msg)
   {
       System.exit(2);
   }
}

and a static method:

static public Handler getExitHandler()
{
    return handler;
}

and in each class I obtain this handler and send it a message when I want to exit:

MyMainActivity.getExitHandler().sendEmptyMessage(0);

again, not so clean, but the same about System.exit(2);

MByD
  • 135,866
  • 28
  • 264
  • 277
  • I tried this one, but actually it can still go back to the main activity. – Androider Mar 29 '11 at 20:25
  • So do it from main activity (with handler for example). – MByD Mar 29 '11 at 20:26
  • well you might not be at the main activity when you decide to take the action, it might appear at the end. The main resume() method could contain this however, it would need to check some state. Anyway I think Android might just think it crashed and try to restart it. – Androider Mar 29 '11 at 20:38
  • @Androider - using an handler you can get the main activity from anywhere you want, and regarding the crash, System.exit(2) will tell it that the application was finished OK, and no reason to restart it (I actually used it today, it solved a big problem I had...) – MByD Mar 29 '11 at 20:44
  • Well, I will try System.exit(2) and see what the effects are. – Androider Mar 29 '11 at 20:49
  • The effect of issuing a System.exit(2) is to send my app to main activity Login. – Androider Mar 29 '11 at 21:13
  • Out of curiosity, have you done it from the main activity or from the main activity as I suggested? I just tried it again, and got no problem, the application has got shut down and the log says `Process 2120 exited cleanly` – MByD Mar 29 '11 at 21:20
  • I issued a custom close_action received in in onCreate of main activity and called finish from there. Also called System.exit(2) without effect. Stays on main page. Seems because I am in create method perhaps. – Androider Mar 29 '11 at 21:34
  • How exactly are you accessing main activity and doing the close out? – Androider Mar 29 '11 at 21:34
  • I will edit my answer to explain, since it's more than one LOC. – MByD Mar 29 '11 at 21:35
  • Please follow up so this question does not need to be revisited later. I want to nail this question once and for all. – Androider Mar 29 '11 at 22:04
  • It still treats it as a crash because I have a running service. – Androider Mar 29 '11 at 23:44
  • Need a way to reliably stop the service somehow before this will work for apps that have services. – Androider Mar 29 '11 at 23:55
1

You could pop up a dialogue that informs the user: "the app will close in x seconds" and start a timer that calls finish() on the activity when it ends.

Gonzo
  • 155
  • 8
  • This would make it more user friendly, but it would need to be combined with removing clearing the stack since another activity can still open. – Androider Mar 29 '11 at 20:26
1

Well, there is a reason why everyone says that, and is the same as "you don't close a web page". I don't know what your users might expect, but this is the way Android/iOS works.

I've used, for something like what you want to do, the flag ACTIVITY_CLEAR_TOP (you set the flag on the intent to open the activity which contains the close button), so in this way the activity stack is deleted, only the current activity remaning: so when you do finish(); the app "closes".

Intent intent = new Intent(this, Some.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
ferostar
  • 7,074
  • 7
  • 38
  • 61
  • I think I understand this one. – Androider Mar 29 '11 at 20:24
  • To make this work on a single click from an activity, this might need to be sent to a hidden activity that would do the close out. – Androider Mar 29 '11 at 20:29
  • Without hidden activity, I guess set a custom close action on the same intent that sets FLAG_ACTIVITY_CLEAR_TOP, and catch in some existing activity. – Androider Mar 29 '11 at 20:42
  • I just did a test issuing this intent to the main activity. Doing a finish() upon receiving the intent in the create does not close out the activity ... it stays on that same activity. – Androider Mar 29 '11 at 21:08
  • I also tried as you suggested and sent to another activity using the intent flag and pressing a done button. The effect is that it went back to the activity that issued the startActivity() ... – Androider Mar 29 '11 at 21:54
  • Its it also necessary to do a System.exit() ? – Androider Mar 29 '11 at 21:55
  • So I issued this intent exactly as you described sending it to an activity with a close app button. When I click on this it goes back to the activity that issued the startActivity(intent), if in addition I do a finish right after calling startActivity(intent) it goes back to first main activity. Now I am going to try putting the button in the main activity and see what happens. – Androider Mar 29 '11 at 22:02
  • BTW, the reason this might need to be done, and in my case is a dedicated tablet device, all touch screen. – Androider Mar 29 '11 at 22:03
  • I do get why is it not something normally done for a phone app. – Androider Mar 29 '11 at 22:06
  • Please note that I have not been able to verify that this does the close or even hides all activities yet. Have you actually implemented this? If so could you please elaborate. – Androider Mar 29 '11 at 23:25
  • Theoretically it does look like the best way, however so far it is not holding up under test. – Androider Mar 29 '11 at 23:26
  • I don't know at this point. Maybe I need to broadcast a shutdown intent, and just handle it at each level. – Androider Mar 29 '11 at 23:46
  • So i guess your solution does not work when there is a service involved. After all you are not finishing the service even when you do finish the main activity. Something more needs to be done. – Androider Mar 29 '11 at 23:53
  • I will try issuing a stopService before and see if this makes a difference. Can you at least comment on the circumstances in which you have seen this approach working? – Androider Mar 30 '11 at 01:40