开发者

Dynamically Specify the First Activity

开发者 https://www.devze.com 2023-01-29 22:14 出处:网络
The main activity is specified in AndroidManifest.xml with: <intent-filter> <action android:name=\"android.intent.action.MAIN\" />

The main activity is specified in AndroidManifest.xml with:

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:n开发者_运维问答ame="android.intent.category.LAUNCHER" />
        </intent-filter>

But I don't know how to dynamically specify the first activity according code logic.


Specify a default Activity which contains the logic to start whichever Activity you want within its onCreate method.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // CODE HERE TO CREATE INTENT
    startActivity(intent);
    finish();
}


@Ian G's answer is correct - but I think his can be improved by removing the launch activity from the backstack. This is what @gardarh is trying to say.

I have a full answer at this related question that basically shows both sides of the problem, activity code & manifest code:

  • Conditionally set first activity in Android

The activity code is basically the same as @Ian's, so I won't repost that. But I will include my addition to his answer.


Manifest Declaration (note the noHistory="true" parameter):

    <activity
        android:name=".activity.EntryActivity"
        android:launchMode="singleInstance"
        android:noHistory="true" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>


I don't have points to add comments but the removing the launcher activity from the back stack might be useful in some cases. This SO post explains how to do this: Removing an activity from the history stack

0

精彩评论

暂无评论...
验证码 换一张
取 消