Last active
August 31, 2024 08:02
-
-
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".
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
intent.addCategory(Intent.CATEGORY_LAUNCHER) and intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) are very important.