Skip to content

Instantly share code, notes, and snippets.

@comoc
Last active August 31, 2024 08:02
Show Gist options
  • Save comoc/6260385 to your computer and use it in GitHub Desktop.
Save comoc/6260385 to your computer and use it in GitHub Desktop.
Launch an external activity with its package and class name. For example, the package name is "com.example.hello", and the class name is "com.example.hello.MainActivity".
/**
* Launch an activity.
* @param context The context to use. Usually your Application or Activity object.
* @param packageName The name of the package that the component exists in. Can not be null.
* @param className The name of the class inside of pkg that implements the component. Can not be null.
* @see {ANDROID_SDK}/samples/android-{VERSION}/Home/src/com/example/android/home/Home.java
*/
private void launch(Context context, String packageName, String className) {
if (context == null || packageName == null || className == null)
return;
ComponentName component = new ComponentName(packageName, className);
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setComponent(component);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
try {
context.startActivity(intent);
} catch (Exception e) {
Log.v(TAG, e.toString());
}
}
@comoc
Copy link
Author

comoc commented Aug 19, 2013

intent.addCategory(Intent.CATEGORY_LAUNCHER) and intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) are very important.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment