Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the result from OnActivityResult inside another class?(outside of the activity

Tags:

android

I start the Voice recognition activity in a non activity class (by passing in the activity) here is the code:

private static void startVoiceRecognitionActivity() {
        // TODO Auto-generated method stub
        Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
                "Talk");
                myActivity.startActivityForResult(intent, REQUEST_CODE);
    }

last line myActivity is the activity i passed in to the class which has this method in.

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    for (final EditText editText : editTextHandlingList) {
        if (requestCode == REQUEST_CODE && resultCode == theActivity.RESULT_OK) {
            ArrayList<String> results = data
                    .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
                        //log the result            }
    }
}

Now the problem is onActivityResult method. I want to be able to get the result back inside the same class and not in the activity. If it is vague please ask me questions..

As I pass the activity to this class is there any way that i can do this? There should be some way to handle this outside.. If you have any questions please ask me.

like image 289
Vivere_FlowCoder Avatar asked Nov 24 '25 05:11

Vivere_FlowCoder


1 Answers

I know this is an old question but with the help of new api its simple:

myActivity.registerForActivityResult(StartActivityForResult()) { result ->
    if (result.resultCode == Activity.RESULT_OK) {
        // There are no request codes
        val data: Intent? = result.data
        doSomeOperations()
    }
}.launch(yourIntent)

like image 113
Saman Sattari Avatar answered Nov 25 '25 19:11

Saman Sattari



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!