Search code examples
android-studioandroid-activitysharedpreferencesonbackpressed

How to clear/reset SharedPreference from an Activity using onBackPressed() Method from another Activity?


So I have SharedPreference coded into (Activity 1) which allows the user to choose which activity is presented to them first on App Launch. The code works fine but what if the user wants to change their selection? When the user presses the back button to go back to (Activity 1), it automatically redirects them to the activity that they previously selected. How can I reset/clear the sharedpreference in (Activity 1) when the user clicks the "back button" on Activity 2 which calls the onBackPressed() Method?

Activity 1

final SharedPreferences sharedPref = getPreferences(MODE_PRIVATE);
    int choice = sharedPref.getInt("default_activity", -1);


    if (choice == -1) {
        // show the option to choose the default activity to the user
        // e.g. dialog with list, then save the corresponding choice to
        // shared preference
        String[] activities = { "Activity 1", "Activity 2", "Activity 3" };

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setAdapter(
                new ArrayAdapter<String>(this,
                        android.R.layout.simple_list_item_1, activities),
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        SharedPreferences.Editor editor = sharedPref.edit();
                        editor.putInt("default_activity", which);

                        editor.apply();
                        launchActivity(which);
                    }
                }).show();
    } else {
        // start the activity and close this activity
        launchActivity(choice);
    }


}

private void launchActivity(int choice) {
    switch (choice) {
        case 0:
            startActivity(new Intent(this, Activity_1.class));
            break;
        case 1:
            startActivity(new Intent(this, Activity_2.class));
            break;
        case 2:
            startActivity(new Intent(this, Activity_3.class));
            break;
    }
    finish();
}

Activity 2

@Override
public void onBackPressed() {
    super.onBackPressed();
    Intent i = new Intent (getApplicationContext(), PreActivity.class);
    startActivity(i);
}

Solution

  • After hours of research and tries, I've found the solution.

    Activity 1

    I declared the variable as Static

    public static SharedPreferences sharedPref;
    

    Then I changed the "GetPreferences" line of code

    FROM

    final SharedPreferences sharedPref = getPreferences(MODE_PRIVATE);
    

    TO

    sharedPref = getPreferences(MODE_PRIVATE);
    

    Then I kept everything else as the same "Activity 1"

     sharedPref = getPreferences(MODE_PRIVATE);
     int choice = sharedPref.getInt("default_activity", -1);
    
    
    if (choice == -1) {
        // show the option to choose the default activity to the user
        // e.g. dialog with list, then save the corresponding choice to
        // shared preference
        String[] activities = { "Activity 1", "Activity 2", "Activity 3" };
    
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setAdapter(
                new ArrayAdapter<String>(this,
                        android.R.layout.simple_list_item_1, activities),
                new DialogInterface.OnClickListener() {
    
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        SharedPreferences.Editor editor = sharedPref.edit();
                        editor.putInt("default_activity", which);
    
                        editor.apply();
                        launchActivity(which);
                    }
                }).show();
    } else {
        // start the activity and close this activity
        launchActivity(choice);
       }
    }
    
    
    
    private void launchActivity(int choice) {
        switch (choice) {
            case 0:
                startActivity(new Intent(this, Activity_1.class));
                break;
            case 1:
                startActivity(new Intent(this, Activity_2.class));
                break;
            case 2:
                startActivity(new Intent(this, Activity_3.class));
                break;
        }
        finish();
    }
    

    Then in Activity 2 & 3 I added a single line of code to the onBackPressed() Method

    Activity 2 & 3

    public void onBackPressed() {
            super.onBackPressed();
            Activity_2.sharedPref.edit().clear().commit(); // This line references 
                                          //"sharedPref" from the activity (Activity 2)
            Intent i = new Intent(getApplicationContext(), Activity_1.class);
            startActivity(i);
    }