Android: leaving the application with the home button and returning to another action with a long press on the home button

I have an application that uses bluetooth and should not be accessible if Bluetooth is disabled on the device.

The way I decided to implement this is as follows:

  • created a dispatcher activity that starts when the application first starts.
  • this activity checks the status of bluetooth, if bt is off, it sends you on noBtScreen, if it is on, it puts you onyesBtScreen

the problem is that when the user gets to noBtScreenand then presses the home button, changes the bt status and returns to the application (long press the home button and choosing my application), he comes in noBtScreen, to which he should not get into this moment.

There are, of course, naive ways to fix this, for example, I can check the status of bt in activity onResume, but I think there is a “right” solution that should be used here.

I tried some of the activity parameters in the manifest file in particular, I tried to put the following flags on NoBtTask:
android:finishOnTaskLaunch
android:allowTaskReparentingin combination and not in combination with android:clearTaskOnLaunch
android:alwaysRetainTaskState

this.finish noBtActivity::onStop, ( , , , , , , , , :

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

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

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

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

09-21 17: 54: 49.765: INFO/ActivityManager (115): 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): : Intent {act = android.intent.action.MAIN cat = [android.intent.category.HOME] flg = 0x10200000 cmp = com. android.launcher/com.android.launcher2.Launcher} 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: / (12603): NoBtActivity.onStop

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

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

+5
8

android:noHistory="true" noBtScreen yesBtScreen.

android docs noHistory:

"" , . , .

EDIT:

, , , .

startActivity , "randomExtra"

onResume onCreate , intent.hasExtra("randomExtra"), , true, . false, startActivity(new Intent(context, DispatcherActivity.class)

+1

finish() onUserLeaveHint() .

+1

android: launchMode = "singleTask" .

0

, , , , . :

(BluetoothEnforcerActivity), . onResume() Bluetooth. Bluetooth , , Bluetooth . "Quit" "Try Again".

"", , BluetoothEnforcerActivity ( ). " " , BluetoothEnforcerActivity Bluetooth onResume(). ( Bluetooth - , . , , , .)

/ Bluetooth , BluetoothEnforcerActivity finish().

, , , Bluetooth , BluetoothEnforcerActivity.

0

- , Activity (BTActivity) NoBtActivity, YesBtActivity BTActivity.

, BTActivity Override onRestart bluetooth , , NoBtActivity, YesBtActivity .

0

- :

  • BroadcastReceiver android.bluetooth.adapter.action.STATE_CHANGED ( ), , Bluetooth .

  • onReceive() BroadcastReceiver . sharedpreference .

  • noBtScreen , onResume() , bluetooth , startactivity(yesBtScreen) () noBtScreen.

  • yesBtScreen, bluetooth , startactivity(noBtScreen) () yesBtScreen.

0

, . . - . ( 4 - , , )

:

@Override
protected void onUserLeaveHint() {
    super.onUserLeaveHint();

    Intent intent = getIntent();
    String activity = intent.getStringExtra("activity");
    if (activity == null || !activity.equals("first") || !activity.equals("firsttime")){
        NavUtils.navigateUpFromSameTask(MainActivity.this);
    }else {
        getIntent().removeExtra("activity");
    }
    System.out.println("mainactivity");
}

:

private void returning(){
    Intent intent = new Intent(this, MainActivity.class);
    intent.putExtra("activity","first");
    startActivity(intent);
}

and in my login activity I have this (in onclick for login):

    Intent i = new Intent(LoginSetup.this, MainActivity.class);
    i.putExtra("activity","firsttime");
    startActivity(i);

Thus, every time I move between actions, something is stored for additional purposes, and when I leave the action, this intention is checked to see if it does not hold these intentions. If there are no additional parameters in the intent, you are returned to the login screen.

hope this helps.

0
source

All Articles