Search code examples
javaandroidnfcactivity-lifecycleonbackpressed

How to check if the back button was clicked


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


Solution

  • 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