Reputation: 11
I have a flutter app for where I need to open another mobile application. The existing libraries in flutter have not been helpful because they are either deprecated or they do not return the result, or they do not allow for arguments to be passed.
Hence, I have had to write a code in native android (in MainActivity.kt
) and invoke this in my flutter screen to open the app and get the result. If I do a simple StartActivity
then I am able to open the app and access it. The problem is that I would like to get the result once the app closes. On the basis of my research I understand that I need to setup a RegisterForActivityResult
and then catch the response.
I am facing a NullPointerException
(Please find below the exact description)
Attempt to invoke virtual method 'android.app.ActivityThread$ApplicationThread android.app.ActivityThread.getApplicationThread()' on a null object reference
package com.example.gshalamoblatest
import android.app.Activity
import android.content.Intent
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.result.ActivityResult
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.annotation.NonNull
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel
class MainActivity : FlutterActivity() {
private val CHANNEL = "com.example.gshalamoblatest/openReadAlongApp"
var test = OpenReadAlongApp()
override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler {
// Note: this method is invoked on the main thread.
call, result ->
if (call.method == "openReadAlongApp") {
test.startIntentFunction()
}
}
}
}
class OpenReadAlongApp:ComponentActivity(){
var startForResult = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == RESULT_OK){
val res = result.data
print(res)
} else {
print("Nothing found")
}
}
public fun startIntentFunction(){
val target = "com.google.android.apps.seekh.READBOOK"
val idName = "intent_open_book_id"
val idValue = "sw_2"
val intentCall = Intent(target).putExtra(idName,idValue)
this.startForResult.launch(intentCall)
}
}
Can someone please advise me on the right approach ?
Upvotes: 0
Views: 1064
Reputation: 11
This is the solution that I came up with
package com.org.g_shala
import android.app.Activity
import android.content.Intent
import android.os.Bundle
import androidx.annotation.NonNull
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.android.FlutterFragmentActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugins.GeneratedPluginRegistrant
import android.util.Log
class MainActivity: FlutterActivity() {
private val CHANNEL = "InputChannelName"
private var gResult: MethodChannel.Result? = null
override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler {
call, result ->
if(call.method == "InputChannelName") {
val args = (call.arguments) as List<String>
gResult = null
val target = "The app that I want to open";
val idName = "idThatIRequire";
val idValue = args[0] "<- This is the argument that I get from flutter";
val code = "againSpecificToMyRequirement";
val intentCall = Intent(target).putExtra(idName,idValue).putExtra(code);
startActivityForResult(intentCall,"SomeNumber")
}
}
gResult = result
}
}
override fun onActivityResult(requestCode: Int, result: Int, intent: Intent?) {
super.onActivityResult(requestCode, result, intent)
if (result == Activity.RESULT_OK && intent != null) {
////var data = "Get the intent response"
gResult?.success(data)
} else
gResult?.success("Not successful")
}
}
Upvotes: 1