Md. Robi Ullah
Md. Robi Ullah

Reputation: 2162

How can I start a react native app with another activity(not MainActivity)

I want to make two app icons for a different purpose. Is it possible to call different UI components from a different app icon? And also I am confused what is the difference between "Activity" and "Intent".

I am new to React Native. I need your help badly.

Upvotes: 2

Views: 6616

Answers (1)

Lenoarod
Lenoarod

Reputation: 3610

for android, the react-native starts firstly the action is MAIN activity, you can config it, it means you can modify the activity. in the main activity, you can config the main component, which is registered in the index js.

 <activity
        android:name=".MainActivity"


        android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
        android:label="@string/app_name"
        android:windowSoftInputMode="adjustResize">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
            <action android:name="android.intent.action.DOWNLOAD_COMPLETE" />
        </intent-filter>
    </activity>

  // Main Activity config the firstly start component
  @Override
  protected String getMainComponentName() {
    return "MainComponent";
  }

 // in the rn index.js
 import { AppRegistry } from 'react-native';
 import App from './App';
 AppRegistry.registerComponent('MainComponent', () => App);

like the below code, you can config the which activity to start, which component is the main component.

As for intent and activity, activity is the ui which showed for user, the intent is the common way to start activity, you can read the official Api

if you want to start another activity, you have to write a bridge module, define a method to start activity, then import it in the component where you use. the detail about bridge module you can go to the react-native official site native modules for android

public class UtilModule extends ReactContextBaseJavaModule {


private static Activity ma;


public UtilModule(ReactApplicationContext reactContext) {
    super(reactContext);
}


public static void initUtilModule(MainActivity activity) {
    ma = activity;
}

@Override
public String getName() {
    return "UtilModule";
}



@ReactMethod
public  void  startActiviy() {
    Intent intent = new Intent(ma,MailActivity.class);
    ma.startActivity(intent);
}

}

Upvotes: 1

Related Questions