Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: leaving an app with home button and returning to a different activity when going through a long press on the home button

I have an app which has uses bluetooth, and should not be accessible if the bluetooth is turned off on the device.

the way I chose to implement this is as follows:

  1. created a dispatcher activity which is launched when the app is first launched.
  2. this activity checks the status of bluetooth, if bt is off, it send you to noBtScreen, if it is on, it takes you to yesBtScreen

the problem is that when the user gets to the noBtScreen and then hits the home button, changes the bt status and comes back to the app (by long pressing the home button and selecting my app) it arrives at the noBtScreen which it shouldn't get to at this point.

There are obviously naive ways to fix this, for example, I can check the bt status in the activity's onResume, but I think that there is a "right" solution which should be used here.

I've tried some of the activity settings in the manifest file specifically, I tried putting the following flags on the NoBtTask:
android:finishOnTaskLaunch
android:allowTaskReparenting in combination and not in combination with
android:clearTaskOnLaunch
android:alwaysRetainTaskState

I also tried adding this.finish to the noBtActivity::onStop method, but that didn't help either (what happened then was that I got in once, got out, and when i got in again, nothing happened and i stayed on the home screen, when i tried it again, it indeed took me to the dispatcher activity, it's interesting to see the log for this:

09-21 17:54:49.511: INFO/ActivityManager(115): Starting: Intent { cmp=com.test.elad/.NoBtActivity } from pid 12603

09-21 17:54:49.523: ERROR/Elad(12603): NoBtActivity.onCreate

09-21 17:54:49.527: ERROR/Elad(12603): NoBtActivity.onStart

09-21 17:54:49.527: ERROR/Elad(12603): NoBtActivity.onResume

09-21 17:54:49.765: INFO/ActivityManager(115): Displayed com.test.elad/.NoBtActivity: +248ms

09-21 17:54:51.867: ERROR/Elad(12603): NoBtActivity.onSaveInstanceState

09-21 17:54:51.867: ERROR/Elad(12603): NoBtActivity.onPause

09-21 17:54:51.867: INFO/ActivityManager(115): Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.HOME] flg=0x10200000 cmp=com.android.launcher/com.android.launcher2.Launcher } from pid 115

09-21 17:54:51.882: VERBOSE/RenderScript_jni(195): surfaceCreated

09-21 17:54:51.882: VERBOSE/RenderScript_jni(195): surfaceChanged

09-21 17:54:52.277: ERROR/Elad(12603): NoBtActivity.onStop

09-21 17:54:56.183: INFO/ActivityManager(115): Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10100000 cmp=com.test.elad/.DispatcherActivity } from pid 115

09-21 17:54:56.265: ERROR/Elad(12603): NoBtActivity.onDestroy

like image 337
ekatz Avatar asked Nov 04 '22 14:11

ekatz


2 Answers

Call finish() in onUserLeaveHint() callback of the activity.

like image 138
Ronnie Avatar answered Nov 11 '22 12:11

Ronnie


Put android:noHistory="true" in Manifest for both noBtScreen and yesBtScreen.

Here's what the android docs says about noHistory:

A value of "true" means that the activity will not leave a historical trace. It will not remain in the activity stack for the task, so the user will not be able to return to it.

EDIT:

I have another suggestion that hopefully will work.

When you startActivity from your dispatcher Activity, you could pass an extra with the key name "randomExtra"

Then in the onResume or onCreate of your other Activities check if intent.hasExtra("randomExtra"), and then if that returns true, then just continue. If it returns false, then do startActivity(new Intent(context, DispatcherActivity.class)

like image 30
Reed Avatar answered Nov 11 '22 11:11

Reed