Search code examples
androidandroid-layoutswitch-statementapplication-settings

By the "Settings Activity" go and change the main activity to another activity


In the Activity Settings shown below, in general, the switch, i want when is true to have "on"/"appear"/"display"/"launch" activity_main_one to the screen and when it is false to have on activity_main_two to the screen:

enter image description here

Any ideas?


Solution

  • private void loadPref() {
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
    
        boolean isTrue = sharedPreferences.getBoolean("Guest_Mode_Key", true);
        if (isTrue) {
            View Guest =  findViewById(R.id.guest_include);
            View User = findViewById(R.id.user_include);
            Guest.setVisibility(View.VISIBLE);
            User.setVisibility(View.GONE);
    
    
        } else {
            View Guest =  findViewById(R.id.guest_include);
            View User = findViewById(R.id.user_include);
            Guest.setVisibility(View.GONE);
            User.setVisibility(View.VISIBLE);
        }
    }
    

    .

    <include
        android:id="@+id/guest_include"
        layout="@layout/content_one"
        android:visibility="visible" />
    
    <include
        android:id="@+id/user_include"
        layout="@layout/content_two"
        android:visibility="gone" />
    

    I created two <*include> tags and set the first to "Visible" and the second "Gone", i found how to get the data from the settings activity it was simple and then i created an if when the switch is true etc. and for the end i just call loadPref()

    @DaveNOTDavid - I was lucky after all ;)