Ali Haghighi
Ali Haghighi

Reputation: 143

pass OnActivityResult data back to hybrid app from capacitor

I'm developing a Compacitor hybrid app. I'm trying to follow instructions in this page: https://capacitor.ionicframework.com/docs/plugins/android , to startActivityForResult (from hybrid javascript part), do something in the native secondActivity and get result back to hybrid part. here is similar problem asked somewhere else without answer! https://github.com/ionic-team/capacitor/issues/1044 this is a brief description about my app: an Ionic blank app + capacitor: this is code in my home.page.ts

import {Component} from '@angular/core';
import {Plugins} from '@capacitor/core';
//typeface
declare global {
    interface PluginRegistry {
        PluginTest2 ? : PluginTest2;
    }
}
interface PluginTest2 {
    goToActivity(): Promise < any > ;
}
@Component({
    selector: 'app-home',
    templateUrl: 'home.page.html',
    styleUrls: ['home.page.scss'],
})
export class HomePage {
    goToSecondActivity() {
    const {PluginTest2} = Plugins;
        PluginTest2.goToActivity().then((result) => {
            // I want data from SecondActivity back here and log it!
            console.log(result);
        })
    }
 }

and the home.page.html is just a button to activate goToSecondActivity(); method. after opening the capacitor project into android studio, I've added this class:

package com.alihaghighicapacitor.qom;
import android.content.Intent;
import com.getcapacitor.JSObject;
import com.getcapacitor.NativePlugin;
import com.getcapacitor.Plugin;
import com.getcapacitor.PluginCall;
import com.getcapacitor.PluginMethod;

@NativePlugin()
public class PluginTest2 extends Plugin {
    @PluginMethod()
    public void goToActivity(PluginCall call) {
        Intent intent = new Intent(getActivity().getApplicationContext(), secondActivity.class);
        startActivityForResult(call, intent, 1);
        // I can return this sillyData! back to "home.page.ts" but I want data from seconfActivity not this class
        JSObject ret = new JSObject();
        ret.put("added", "sillyData!");
        call.success(ret);

    }
    @Override
    protected void handleOnActivityResult(int requestCode, int resultCode, Intent data) {
        super.handleOnActivityResult(requestCode, resultCode, data);
        String recievedMessage = data.getStringExtra("Data");
        // capacitor team tutorial in the page I mentioned earlier instructs to use PluginCall as follows but IT DOES NOT WORK FOR ME!
        PluginCall savedCall = getSavedCall();
        if (savedCall == null) {
        return;
        }
       if (requestCode == REQUEST_IMAGE_PICK) {
       // Do something with the data
       }
    }
}

here is the secondActivity.java:

package com.alihaghighicapacitor.qom;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class secondActivity extends AppCompatActivity {
    EditText editText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        editText = findViewById(R.id.editText);
    }

     public void sendMeHome(View view) {
        Intent intent = new Intent();
        String message = editText.getText().toString();
        intent.putExtra("Data", message);
        finish();
    }
}

and relevant part of activity_second.xml

<Button android:id="@+id/button"
android:onClick="sendMeHome"/>
<EditText android:id="@+id/editText"/>
//clicking the button should get the editText value and send it back to
=> previous activity => to home.page.ts => and log it

this code fires secondActivity but no data is received in native part except the sillyData I commented in the code above! if anyBody can help it would be a great help for me. here is the MAinActivity.java:

 package com.alihaghighicapacitor.qom;
 import com.alihaghighicapacitor.qom.PluginTest2;
 import android.os.Bundle;

 import com.getcapacitor.BridgeActivity;
 import com.getcapacitor.Plugin;

 import java.util.ArrayList;

 public class MainActivity extends BridgeActivity {
   @Override
   public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);

     // Initializes the Bridge
     this.init(savedInstanceState, new ArrayList<Class<? extends Plugin>>() {{
       // Additional plugins you've installed go here
       // Ex: add(TotallyAwesomePlugin.class);
       add(PluginTest2.class);

     }});
   }

 }

Upvotes: 2

Views: 4145

Answers (2)

JWS
JWS

Reputation: 21

For handleOnActivityResult() to be called, you need to register your intent request codes with @NativePlugin.

@NativePlugin(
  requestCodes={MyPlugin.REQUEST_IMAGE_PICK}
)

Intents with Result(s)

Upvotes: 2

jcesarmobile
jcesarmobile

Reputation: 53321

Before you return silly data you have to save the call with saveCall(call);, otherwise savedCall will be null and it will go to the empty return instead of to the code where you handle the returned data.

Upvotes: 1

Related Questions