问题
I have the following manifest.
<application
android:name=".MyMainApplication"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".MyMainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
I also have the following global static function.
MyMainApplication application = MyMainApplication.instance();
as technique described in Using Application context everywhere?
I was wondering, out from application
variable, is it possible for me to get MyMainActivity
?
回答1:
There is no way to retrieve the application's main Activity
using the Application
instance.
Usually there is no reason to do this... perhaps there is a better solution than manipulating the main Activity
directly as you apparently are doing. Maybe you should update your post explaining specifically what you are attempting to do.
回答2:
You can start the activity form the MyMainApplication class though it doesn't extends Activity
In Main Application Class create new Intent and set the intent flags to Intent.FLAG_ACTIVITY_NEW_TASK
. Activity gets instantiated. I have tested and verified.
code snippet for reference :
Intent intent = new Intent(this,MyMainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
来源:https://stackoverflow.com/questions/11380322/getting-main-activity-from-main-application