-4

tl;dr: Is the approach at the bottom of this question a reasonable approach to exiting an Android application?


I maintain an Android music player application. Source.

I've gotten some requests for an 'exit button to be integrated into the player. Issue. I've added an exit option to the menu of each Activity, e.g.

enter image description here

I've tried a few approaches to exiting the application, including:

  • Send an intent with an "exit" extra to my first Activity reference.
  • Broadcast an "exit" message as described here.

Both of these approaches had some issues. The first approach never worked - I'd call startActivity as described in the example, but the intent received by the Activity never included my "exit" flag.

The second approach worked most of the time, but if I allowed Android to destroy Activities on my back stack, then clicked exit, the activities would be re-launched (apparently after receiving the "exit" broadcast). I could consistently re-create this problem by using the developer option to destroy all Activities immediately after they leave the view.

The approach I settled on was to broadcast an "exit" message and explicitly launch the home screen:

if (id == R.id.action_exit) {
    // I had issues just broadcasting the "exit" intent
    // if the other activities on the stack had finished, 
    // they were re-created, presumably *after* the broadcast had been sent
    Intent broadcastIntent = new Intent();
    broadcastIntent.setAction("com.smithdtyler.ACTION_EXIT");
    sendBroadcast(broadcastIntent);
    // To make sure we exit, *first* send the broadcast (this makes sure the MusicPlaybackService shuts down)
    // then launch the home screen 
    Intent startMain = new Intent(Intent.ACTION_MAIN);
    startMain.addCategory(Intent.CATEGORY_HOME);
    startMain.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(startMain);
    finish();
    return true;
}

The full source code to my exit approach is available here.

This approach has been working in testing, but I'm curious if there's a possible user configuration I could be overlooking where this could cause problems.

Community
  • 1
  • 1
T.D. Smith
  • 984
  • 2
  • 7
  • 22

1 Answers1

0

exiting an Android application wise?

In most cases, any exit on Android is not justified and is not needed. In your case it'd be most likely sufficient to remove on-going notification whenever user hit "stop" (or add "X" button to get rid of notification on user demand).

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
  • Why is it not justified or needed? – T.D. Smith Feb 10 '15 at 14:13
  • Because concept of app exiting makes no sense on android. If your app is properly written it will be "sleeping" unless required again (unless it does something intentionally in background, like plays music etc). There's no point of exiting the app to free anything. If things are done right, Android will take care of flushing out your app **when really needed**. – Marcin Orlowski Feb 10 '15 at 14:16
  • In my case I felt it was necessary to avoid a two-step "exit". My application has a background service (playing music) and users wanted to be able to stop that service and exit the app. If I didn't explicitly exit my application, the user would be forced to 1. stop the service 2. Press "home". I'd like to give them a single button which does both tasks. Admittedly two steps isn't much worse than one, but I'm not seeing a downside to exiting the app other than it breaks Android conventions. – T.D. Smith Feb 10 '15 at 14:24
  • Thinking about your comment some more, I think you're right. I'll change my exit button to "stop" and just have it stop the service and let the user press "home" or whatever else they want to do. – T.D. Smith Feb 10 '15 at 14:47
  • I recommend checking how other android music players works - the only think you can do there to "exit" is stop playback. If you pause, it is paused (and all things, like notification etc stays). If use stop playback (either from notification or main UI) and press home he basically "quit" your app. If he stopped from notification, then it's one press less but effect is the same – Marcin Orlowski Feb 10 '15 at 14:53
  • I ultimately decided to go with the original approach that I posted. I tried just having a button to "stop" the service, but too much of my UI depends on the service running, so stopping it without closing the UI didn't make sense. Anyway, thanks for the feedback. – T.D. Smith Feb 11 '15 at 01:51