1

I have an app with a button. (see image). This preview only occurs if the user has not activated a certain setting. If he presses the button, he arrives in the settings. If he presses back he comes back to the settings. I would now like to intercept this "back".
This back button is directly on the smartphone. How can I intercept this activity? I have found something, unfortunately it does not give me this. Do I have something wrong? or is there something else? Thanks in advance.

  public class NfcSettingActivity extends AppCompatActivity {

    private Dialog epicDialog;
    private Button btn_nfc_navigate_setting;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_nfc_setting);
        btn_nfc_navigate_setting = findViewById(R.id.btn_nfc_navigate_setting);
        btn_nfc_navigate_setting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(Settings.ACTION_NFC_SETTINGS));
            }
        });

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        switch (item.getItemId()) {
            case android.R.id.home:
                onBackPressed();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        System.out.println("back pressed");
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            onBackPressed();
        }
        return super.onKeyDown(keyCode, event);
    }
}

enter image description here

CoolMind
  • 26,736
  • 15
  • 188
  • 224
  • in not working the override??? – ΦXocę 웃 Пepeúpa ツ May 28 '20 at 09:29
  • Is it `Activity` or `Fragment`? – CoolMind May 28 '20 at 09:30
  • Unfortunately not. I don't see the output on the console –  May 28 '20 at 09:30
  • @CoolMind Activity –  May 28 '20 at 09:31
  • You are using settingsActivity rt? and you want to invoke onBackPressed in this activity ? – Shalu T D May 28 '20 at 09:38
  • 1
    You are using println. You need to use log instead. Like this: `Log.d("some tag", "some message");` for more info go here: https://developer.android.com/studio/debug/am-logcat – ali73 May 28 '20 at 09:48
  • @ali73 I Should still see this on my console? –  May 28 '20 at 09:49
  • @ShaluTD what do you mean? –  May 28 '20 at 09:49
  • @FischerKlaus Go to the link and you will find every thing you need there But for a short answer, I should say YES – ali73 May 28 '20 at 09:50
  • @FischerKlaus, yes, ali73 hints you. Use `Log` for logging (or `Timber` library). – CoolMind May 28 '20 at 09:51
  • After the user has clicked back, I would like to check whether he has still not activated the settings. And deal with a different case depending on the case. Unfortunately, this doesn't work either. –  May 28 '20 at 09:53
  • Can it work with `startActivityForResult`? I mean https://stackoverflow.com/questions/14957691/how-to-enable-nfc-setting. – CoolMind May 28 '20 at 09:56
  • May `startActivity(new Intent(Settings.ACTION_NFC_SETTINGS));` is the wrong activity to start that? –  May 28 '20 at 09:56
  • @CoolMind `startActivityForResult` doesn't work either –  May 28 '20 at 10:02
  • I don't know, saw several links, most of them use the same, for instance, https://stackoverflow.com/questions/5945100/android-changing-nfc-settings-on-off-programmatically. See https://stackoverflow.com/questions/7618657/how-to-check-whether-nfc-is-enabled-or-not-in-android and https://developer.android.com/guide/topics/connectivity/nfc/nfc for taking a status. – CoolMind May 28 '20 at 10:02
  • It can't be because of this code snippet since I tried it. Must have another bug :D –  May 28 '20 at 10:10
  • Well, what doesn't work right? Does a back button work? Does `onBackPressed` work? Does `Log` print a string? Does NFC settings show? – CoolMind May 28 '20 at 10:16
  • 1
    I press the button and I jump to the NFC settings. If I now press the back button I get back to this screen where I was before. Unfortunately, it doesn't show me that he executed this method `onBackPressed ()` . So he doesn't do this method. –  May 28 '20 at 10:21
  • 1
    So the method works like this, as soon as I press the back button from the window it executes it. But when I press the back button of the settings, nothing happens. I would like to have a method that tells me whether it came from this back button. –  May 28 '20 at 10:25
  • You cannot catch the back button event while you are in the settings page. Please check my answer for proper handling, thru activity lifecycle's onResume. – user1506104 May 28 '20 at 10:26
  • Did you try to debug the app? Doesn't it stop at break point in `onBackPressed`? Maybe you should show, how you call `NfcSettingActivity` and what is in `AndroidManifest`. Or see the answer of user1506104. – CoolMind May 28 '20 at 10:27

3 Answers3

1

Probably you forgot to add super.onBackPressed().

override fun onBackPressed() {
    ...
    super.onBackPressed()
}

UPDATE

Probably you should use onOptionsItemSelected:

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    switch (item.getItemId()) {
        case android.R.id.home:
            onBackPressed();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

And even maybe onKeyDown:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        onBackPressed();
    }
    return super.onKeyDown(keyCode, event);
}
CoolMind
  • 26,736
  • 15
  • 188
  • 224
  • @FischerKlaus, see https://stackoverflow.com/questions/39532594/android-onbackpressed-is-not-being-called. Have you used `onKeyDown`, `onKeyUp` in your code? Or `onOptionsItemSelected`? – CoolMind May 28 '20 at 09:37
  • no I didn't use that! I will check the qeustion. Thanks –  May 28 '20 at 09:39
  • Many thanks. Tried the new answer. Unfortunately, it still doesn't work. :( –  May 28 '20 at 09:46
  • I edited my question. Maybe if have something more wrong. –  May 28 '20 at 09:47
1

This is how I understand your problem:

NfcSettingActivity only shows when a certain setting (probably NFC) is not activated. If not set, the user can click your button in the activity and will take the user to the NFC Setting of Android. Once activated, the user clicks back button and takes him/her back to the NfcSettingActivity. At this point, you want to know if the NFC setting was successfully activated.

Here is what you need to do: You don't need to catch back button press. What you need to do is check if the NfcSettingActivity is in resumed state again. In NfcSettingActivity, you need to have the following:

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

    // Do your NFC checking here!
    // Also, you might need to add a flag here to check if the user has been to the NFC Setting page already!
}

Understand the Activity Lifecycle

user1506104
  • 6,554
  • 4
  • 71
  • 89
  • Ohh my god! Thank you very much! I had the wrong method! No wonder it doesn't work. A big thank you for taking the time. –  May 28 '20 at 10:28
  • This is not a very good method of trying to detect that the user has turned NFC on, because if instead of clicking the button to start `Settings.ACTION_NFC_SETTINGS` the user goes to the quick settings to turn NFC on instead then you `NfcSettingActivity` with never be paused and thus not resumed. Much better to use a Broadcast Receiver to detect NFC settings state changes. – Andrew May 28 '20 at 11:04
  • That is true. OP needs to consider this scenario. However, this answer operates only on the fact that the OP specifically wants to bring the user to the NFC Setting page instead of the quick setting. – user1506104 May 28 '20 at 11:16
0

Just try writing what you want on back press before calling super.onBackPressed(); in the overriden method onBackPressed()

@Override
public void onBackPressed() {
    System.out.println("back pressed");
    super.onBackPressed();
}

Also use log instead of println("")