Define Default Activity (when app starts) programmatically

大兔子大兔子 提交于 2019-11-28 21:58:38

Instead of ActivityA, consider using wrapper activity to be called from launcher. You will eliminate a need for checking for ACTION_MAIN. Also you can store target activity name in preferences and use it to directly start your target activity via different intent signature:

public Intent (String action)

 <activity class=".StartActivity" android:label="...">
             <intent-filter>
                 <action android:name="android.intent.action.MAIN" />
                 <category android:name="android.intent.category.LAUNCHER" />
             </intent-filter>
 </activity>

 <activity class=".ActivityA" android:label="...">
             <intent-filter>
                 <action android:name="mypackage.ActivityA" />                    
             </intent-filter>
 </activity>

And in StartActivity

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    settings = PreferenceManager
            .getDefaultSharedPreferences(getBaseContext());

    String action = settings.getString("Activitypref","mypackage.ActivityA");
    Intent intent = new Intent(action);
    startActivity(intent);
    ....
}

You may need to fiddle around little bit to get it right.

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