Logan Eldridge
Logan Eldridge

Reputation: 45

How to avoid setting default Boolean when pulling a SharedPreferences

I'm making an app where the setup asks if the app is on the child's phone or the parent's phone and sets up the app according, and it is supposed to save a boolean in the SharePreferences so that it opens to the right app version each time.

I set up the app for parent and it goes to the parent app, but when I open the app again, it opens to the child's app, I'm guessing since I have to put this default boolean in as a backup it is causing this. How can I avoid this problem?

Here is the code I'm using to set the parent boolean to true when parent button is selected:

        // PARENT = TRUE

    parent_app_selection.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(parent_or_child_first_menu.this, LoginActivity.class);

            //Saves a boolean that determines the app to be the parent version of the app.
            SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(parent_or_child_first_menu.this);
            prefs.edit().putBoolean("parent", true).apply();

            startActivity(intent);
        }
    });

And this is the code I'm using to check what the boolean is set to

    SharedPreferences is_it_parent_app = 
    PreferenceManager.getDefaultSharedPreferences(this);
    boolean parent_setup_completed = is_it_parent_app.getBoolean("parent",false);
    if(parent_setup_completed)
    {
        Intent home = new Intent(Permission_requests.this, parents_app.class);
        startActivity(home);
    }

I don't want to set the boolean backup to be true because I don't want the child to be able to get into the parent's side of the app in case it falls back on that boolean.

Upvotes: 0

Views: 46

Answers (1)

kemdo
kemdo

Reputation: 1459

more 1 line of code to be done

 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(parent_or_child_first_menu.this);
SharedPreferences.Editor editor = prefs.edit();
    editor.putBoolean("parent", true);
    editor.commit()

Upvotes: 1

Related Questions