Disable button after returning from an intent

Viewed 30

Afternoon all I need to call an intent and upon returning disable the button for X seconds. I have tried variations of the following, which either disable the button immediately and then enable or do not do what I need full stop.

        Guarding.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Guarding.setEnabled(false);
                SharedPref.write(SharedPref.SCANTYPE,"GUARDING");
                Intent intent = new Intent(Menu.this, CmxScanner.class);
                startActivity(intent);
                Guarding.postDelayed(new Runnable() {
                    public void run() {
                        Guarding.setEnabled(true);
                        //Log.d(TAG,"resend1");
                    }
                },10000);
            }
        });

So on first entry to the menu i have a button that is enabled, after clicking it must call the intent and upon returning disable the button again for X seconds. Before it will enable and allow a second request to the Intent

1 Answers

You can register your calling activity for a result as explained in the Android docs here. Then in the result callback you simply enable the button after 10 seconds:

ActivityResultLauncher<String> mGetContent = registerForActivityResult(new GetContent(),
    new ActivityResultCallback<Uri>() {
        @Override
        public void onActivityResult(Uri uri) {
            Guarding.postDelayed(new Runnable() {
                public void run() {
                    Guarding.setEnabled(true);
                }
            }, 10000);
        }
    }
});
Related