React native how to set flags for android intent?

Viewed 708

I have this piece of code

import { Linking } from 'react-native'

const action = "android.intent.action.WEB_SEARCH";
const extras = [
    { key: "query", value:"kittens" } 
];
Linking.sendIntent(action, extras);

However, I get Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want? So the fix seems to simply add this flag. How?

In android documentation I found that this flag has constant value 268435456, but I am unsure how to add that to intent.

I tried to set it in extras, but none of these work

{ key: "268435456", value: "" }
    
{ key: "268435456", value: "true" }
    
{ key: "268435456", value: "1" }
    
{ key: "flag", value: "268435456" }
    
{ key: "flags", value: "268435456" }
1 Answers

Unfortunately Linking.sendIntent() isn't really fit for purpose as it doesn't allow for any real control of the intent. Your best bet is to use a library for example expo-intent-launcher:

import intentLauncher from "expo-intent-launcher";

intentLauncher.startActivityAsync("android.intent.action.WEB_SEARCH", {
  extra: {
    query: "kittens"
  }
});
Related