How can the first activity be selected at runtime based on data?

I am just starting out with Android, and I think I missed something. It looks like in Android you decide during development whose activity will be the first to be displayed in your application.

I would like to write my application in such a way that some centralized controller starts up, and it decides which activity should be the first (for example, based on some data received somewhere)

can be done, and if so, how? thank.

+3
source share
4 answers

, , , "" . , , : noHistory = "true" , .

+6

.

AndroidManifest.xml

<activity android:name=".activities.LaunchActivity"
    android:noHistory="true"
    android:theme="@android:style/Theme.NoDisplay">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

<activity android:name=".onboarding.OnboardingActivity"/>

<activity android:name=".activities.MainActivity"/>

LaunchActivity.java

public class LaunchActivity extends Activity {

    public static final String FIRST_APP_LAUNCH = "com.your.package.name";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (isFirstAppLaunch()) {
            setFirstAppLaunch(false);
            startActivity(new Intent(this, OnboardingActivity.class));
        } else {
            startActivity(new Intent(this, MainActivity.class));
        }

        finish();
    }

    private boolean isFirstAppLaunch() {
        SharedPreferences preferences = this.getPreferences(Context.MODE_PRIVATE);
        return preferences.getBoolean(FIRST_APP_LAUNCH, true);
    }

    private void setFirstAppLaunch(boolean value) {
        SharedPreferences preferences = this.getPreferences(Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putBoolean(FIRST_APP_LAUNCH, value);
        editor.apply();
    }
}
+3

, . onCreate , , .

+2

You will find your answer here: how to choose the first activity dynamically

Hope this helps.

+1
source

Source: https://habr.com/ru/post/1790157/


All Articles