1

I have an Activity that I consider a critical operation (Specific communication with another computer over Bluetooth) and I want to make it so that when the user leaves the activity, it cannot be resumed to that state. With other words, if the user resumes the activity it should be recreated.

Since this activity uses Bluetooth it might start one or two activities for result (Enable-Bluetooth activity and Request-Permissions activity) and therefore, I cannot simply finish() the activity in onPause().

By leaving the Activity, I mean presses the home button, takes a phone call or presses the multitask button

I have experimented with some Activity Launch modes (like singleTask) without success.

I already call super.onCreate(null) in the Activity's onCreate() method, preventing it from recreating to a specific state after it has been destroyed, but I want to reset the activity whether onDestroy() has been called or not.

Does anyone have any suggestions on how this should be done correctly?

Edit:

The question in the Possible duplicate explains how to quit an application and it's subtasks completely (whereas just finish() would suit my needs perfectly - if I knew where to call it). This question is about finding a clean way to not resume the previous state of the Activity.

Joakim
  • 3,224
  • 3
  • 29
  • 53
  • Possible duplicate of [How to quit android application programmatically](http://stackoverflow.com/questions/6330200/how-to-quit-android-application-programmatically) – K Neeraj Lal Sep 16 '16 at 14:07
  • suggestion: do not rely on `onDestroy()` for your logic or saving any data. If OS is stressing and many other applications are running, `onDestroy()` might not be called. See this: https://developer.android.com/reference/android/app/Activity.html#onDestroy() – ᴛʜᴇᴘᴀᴛᴇʟ Sep 16 '16 at 14:08
  • @th3pat3l I have not even implemented the `onDestroy` in my application. – Joakim Sep 16 '16 at 14:14
  • I should add that I don't need the Activity to be removed from the Multitask list either. – Joakim Sep 16 '16 at 14:18
  • *I have an Activity that I consider a critical operation (Specific communication with another computer over Bluetooth)* ... Activity is a bad choice for such operations ... Obvious choice is a Service – Selvin Sep 16 '16 at 14:22
  • Yes if it were the Main purpose of the App I'd definetly put it in a service. However it's just a one time operation that will always be performed exactly within this specific acitivy. – Joakim Sep 16 '16 at 14:35
  • What you need is to reset your logic that runs in the activity, right? Can you post some code? How do you handle the configurations changes right now? – masp Sep 16 '16 at 15:27

1 Answers1

1

If you never want a state persist once you've left via the home button, or perhaps even when the screen turns off, the simplest thing is to work with the lifecycle events available. It's a whole lot simpler than trying to work around Androids design by doing things like forcing the close of your app.

Since everything needs to be setup each time someone returns to the app, you can move all of your setup logic out of onCreate and into onResume. Then, perform all the required cleanup (kill your BT connection, etc) in onPause. The only possible gotchas are related to things like changing screen rotation/ opening the keyboard which might trigger lifecycle events that you didn't intend. That might make your program less responsive if you have a lot of long running tasks on the UI thread in onResume.

sarwar
  • 2,835
  • 1
  • 26
  • 30