Android: How to display the main activity?
I'm trying to display my main activity from within another class, but the problem is that I do not know the class name at that point (the code I'm writing is inside an Android library, and the main activities are in the concrete apps).
How can I get the main activities class at runtime or 开发者_开发问答otherwise create an intent that launches the main activity?
I tried this:
new Intent(Intent.ACTION_MAIN);
But nothing happens.
This seems like a perfect fit: PackageManager.getLaunchIntentForPackage()
. Note that it is API level 3 though.
Why not passing the main activity's name when initializing the library when it is used the first time?
Include the following intent in the manifest for the Main activity which you are referring to:
<intent-filter>
<action android:name="com.android.myproject.THIS_IS_MY_ACTION" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
and you can call this class using :
startActivity(new Intent("com.android.myproject.THIS_IS_MY_ACTION"));
can you try implementing some interface(or perhaps abstract class) to describe your main class.
And in the class that you are writing in the library, instantiate an object of the interface.
精彩评论