简体   繁体   中英

Change AndroidManifest.xml to set LAUNCHER activity base on shared preference

I have a registration activity and also a main activity.

What I want to achieve:

I want to:

1) show the registration activity if the user hasn't registered

2) show the main activity if the user has registered

What I did:

1) I have changed the registration activity to be the Main/Launcher activity in the Android.Manifest.xml file:

    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
    </activity>
    <activity
        android:name=".RegistrationActivity"
        android:label="@string/app_name" >

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

Problem:

1) How do I set the Main Activity as the Launcher activity after the the Register Button is pressed for subsequent app launches?

public void btnRegisterPressed (View view) {

        // Save isRegistered flag into Shared Preferences
        SharedPreferences userDetails = this.getSharedPreferences("userDetails", MODE_PRIVATE);
        SharedPreferences.Editor edit = userDetails.edit();
        edit.clear();
        edit.putBoolean("isRegistered", true);

        Intent i = new Intent(this,MainActivity.class);
        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(i);
}

You will need to make a landing page for the app and have the method run in it, if you don't want them to land into the current main. So the run method is the one to go to main.

I prefer to keep my shared preferences as constants for my app.

pref = getSharedPreferences(MyConstants.MY_APP);
    editor = pref.edit();



public void run(View view) {
    String getStatus = pref.getString(MyConstants.REGISTER, "nil");
    if (getStatus.equals("true")) {
        startActivity(new Intent(this, Main.class));
    } else {
        Toast.makeText(this, "Register first",
                Toast.LENGTH_SHORT).show();
    }
}

In your register class:

editor.putString(MyConstants.REGISTER, "true");
    editor.commit();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM