I am trying to open flutter app from an another flutter app using package name only. It is not deep linking but just simply opening the application which is installed in device. Also want to share some data and receive that in flutter app.
My First (Native) application from which I am calling my flutter application with some text data:-
var intent = context.packageManager.getLaunchIntentForPackage(packageName)
if (intent == null) {
intent = Intent(Intent.ACTION_VIEW)
intent.data = Uri.parse("market://details?id=$packageName")
val bundle = Bundle()
bundle.putString("user_id", "1111")
intent.putExtra("data", bundle)
intent.setType("text/plain")
}
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
context.startActivity(intent)
I am using method-channel for that.
class _MyHomePageState extends State<MyHomePage> {
static const methodChannel = MethodChannel("samples.flutter.dev/battery");
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
children: <Widget>[
ElevatedButton(
onPressed: () async {
openApp();
},
child: Text("Please work this time"))
],
),
),
);
}
static Future<bool?> openApp() async {
return await methodChannel.invokeMethod<bool>("getOpenApp");
}
}
Now the Activity class is like this :-
class MainActivity : FlutterActivity() {
//private var sharedText: String? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val intent = intent
val action = intent.action
val type = intent.type
if (Intent.ACTION_SEND == action && type != null) {
if ("text/plain" == type) {
// handleSendText(intent) // Handle text being sent
}
}
}
private val CHANNEL = "samples.flutter.dev/battery"
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
GeneratedPluginRegistrant.registerWith(flutterEngine)
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL)
.setMethodCallHandler { call: MethodCall, result: MethodChannel.Result ->
if (call.method.contentEquals("getOpenApp")) {
var userId = "No data received"
val bundle: Bundle? = intent.getBundleExtra("data");
//val bundle = intent.getBundleExtra("data");
if (bundle!=null){
userId = bundle.getString("user_id").toString()
Log.d("userid","is equals to: $userId")
System.out.print("userid ki value : $userId")
}else{
Log.d("empty","is equals to: empty hai ")
}
result.success(200)
//sharedText = null
}
}
}
}
Please let me know what mistake I am doing here as I am getting empty bundle every time.