A
A
artem2022-01-11 18:42:08
Android
artem, 2022-01-11 18:42:08

How to track what the user clicked in startResolutionForResult()?

61dda3c4c7c4f712156168.jpeg
61dda3cc0768d263437964.jpeg

I want the user to enable geolocation using the system dialog by calling

exception.startResolutionForResult([email protected], 45)


kak-vklyuchit-gps-na-telefone-syaomi1.jpg

In the official documentation, it says that you need to track the result of clicking using ActivityForResult (), but as you know, this method is deprecated. Yes, it may not be removed anytime soon, but I still wanted to stick with "Good Code"

How do I know what the user replied?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
AlexVWill, 2022-01-11
@AlexVWill

you need to track the result of clicking using ActivityForResult (), but as you know, this method is deprecated

// You can do the assignment inside onAttach or onCreate, i.e, before the activity is displayed
ActivityResultLauncher<Intent> someActivityResultLauncher = registerForActivityResult(
        new ActivityResultContracts.StartActivityForResult(),
        new ActivityResultCallback<ActivityResult>() {
            @Override
            public void onActivityResult(ActivityResult result) {
                if (result.getResultCode() == Activity.RESULT_OK) {
                    // There are no request codes
                    Intent data = result.getData();
                    doSomeOperations();
                }
            }
        });

public void openSomeActivityForResult() {
    Intent intent = new Intent(this, SomeActivity.class);
    someActivityResultLauncher.launch(intent);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question