使用共享首选项更改背景

Change background using shared preference

我有一个应用程序允许我的用户使用按钮自定义我的应用程序的背景。我的应用程序是这样工作的:首先,它会将他们带到我的主 activity,那里有一个按钮,他们可以按下该按钮来自定义背景。当他们按下该按钮时,它将引导他们进入一个对话框片段,让用户可以选择他们想要的背景图像。我可以更改我的背景,但是共享首选项无法正常运行。当我关闭我的应用程序并打开它时,它会变回我的默认背景,但是当我按下对话框片段按钮时,它会将背景更新为他们选择的任何背景。

基本上,只有当我打开提供背景图片的按钮时,背景才会更新。

我不确定我是否解释得很好所以这是我的 gif problem

背景只有在我按下地形按钮时才会更新,有人有办法解决这个问题吗?我对 android 和 java 还是很陌生,所以我不确定我是否遗漏了什么...

PopupTheme.java

public class PopupTheme extends DialogFragment implements View.OnClickListener {

    private ImageButton btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btn10;

    private static final String BG_NAME = "bgName";
    private static final String BG_KEY = "bg";

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View view =  inflater.inflate(R.layout.activity_popup_theme, container, false);

        btn1 =  view.findViewById(R.id.btn1);
        btn2 = view.findViewById(R.id.btn2);
  
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
   
        if (getBackground() != R.drawable.bgscreen1 ){
             MainActivity.mainLayout.setBackgroundResource(getBackground());
        }

        return view;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn1:
                MainActivity.mainLayout.setBackgroundResource(R.drawable.bgscreen1);
                Toast.makeText(getContext(), "Clicked", Toast.LENGTH_SHORT).show();

                storeBackground(R.drawable.bgscreen1);
                break;

            case R.id.btn2:
                MainActivity.mainLayout.setBackgroundResource(R.drawable.bgscreen2);
                Toast.makeText(getContext(), "Clicked", Toast.LENGTH_SHORT).show();

                storeBackground(R.drawable.bgscreen2);
                break;
        }
    }

    public void storeBackground(int background) {
        SharedPreferences sharedPreferences = getActivity().getSharedPreferences(BG_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();     //accessing the shared pref
        editor.putInt(BG_KEY, background);
        editor.apply(); 
    }

    //    getting the background
    public  int getBackground() {
    
        SharedPreferences sharedPreferences = getActivity().getSharedPreferences(BG_NAME, Context.MODE_PRIVATE);
        int selectedBG = sharedPreferences.getInt(BG_KEY, R.drawable.bgscreen1); 
        return selectedBG;
    }

}

主要Activity.java

public class MainActivity extends AppCompatActivity {

    private Button btnWatch, btnReadStory, btnFavorites, btnAbout, btnListen;
    private ImageButton btnTheme;

    static ConstraintLayout mainLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnTheme = findViewById(R.id.btnTheme);
        mainLayout = findViewById(R.id.layoutMain);

        btnTheme.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PopupTheme popupTheme = new PopupTheme();
                popupTheme.show(getSupportFragmentManager(), "Popup Theme");

            }
        });
    }

}

问题是您只有在打开 PopupTheme 对话框时才会更新 MainActivity 的背景,因此您需要将 getBackground 和更新代码从 PopupTheme 移动到 MainActivity 上的 onCreate 方法,以便在用户启动时执行代码应用

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

     btnTheme = findViewById(R.id.btnTheme);
     mainLayout = findViewById(R.id.layoutMain);

     int background = getBackground();
     if (background  != R.drawable.bgscreen1 ){
        mainLayout.setBackgroundResource(background);
     }

     // Other code on onCreate
}

// Move getBackground method here from PopupTheme

注意:您可以使用-1作为背景的默认值,并检查是否为-1表示用户使用默认背景