Skip to content

Instantly share code, notes, and snippets.

@vasyl91
Last active September 27, 2018 09:46
Show Gist options
  • Save vasyl91/815a98a14900369fb9ad1bf5b20fc3d7 to your computer and use it in GitHub Desktop.
Save vasyl91/815a98a14900369fb9ad1bf5b20fc3d7 to your computer and use it in GitHub Desktop.
Native module that simulates / programmatically presses Home button in React Native
//Create Native module and paste this code. Then in js call it with: YourClassName.launchHome();
//add <category android:name="android.intent.category.HOME"/> to AndroidManifest.xml (paste between "MAIN" and "LAUNCHER")
import android.content.Intent;
import android.content.Context;
public class YourClassName extends ReactContextBaseJavaModule {
@ReactMethod
public void launchHome() {
Context context = getReactApplicationContext();
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(startMain);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment