Getting Main Activity from Main Application

耗尽温柔 提交于 2019-12-11 11:07:47

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!