Define Default Activity (when app starts) programmatically

前端 未结 1 1285
伪装坚强ぢ
伪装坚强ぢ 2020-12-14 13:03

My application is composed by a few activity.

Activity A is my main menu with some icons. This Activity can launch depending on which icon you press: Activity B,C,D,

相关标签:
1条回答
  • 2020-12-14 13:53

    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.

    0 讨论(0)
提交回复
热议问题