使用共享首选项存储按钮状态有效,但当我离开应用程序时按钮名称会更改

Storing button state with shared preference works but name of button changes when I leave the app

我将按钮的状态存储在共享首选项中,它似乎可以正常工作。我正在使用按钮打开服务。如果我打开服务并离开应用程序,我可以再次按下按钮将其关闭,这很好。唯一的问题是,当我打开服务时,按钮中的文本应该更改为 "stop service" 并且确实如此,但是当我退出应用程序时,它会更改回 "start service" 但状态已保存。这是我实现这个的地方


  SharedPreferences prefs =  Home.this.getActivity().getSharedPreferences("check", MODE_PRIVATE);
  final String check_state = prefs.getString( "state", "default");
  holder.startsCar.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(check_state.equals("false")){
                            holder.startsCar.setText("Stop Car");
                            Intent i = new Intent(Home.this.getActivity(), MQTTService.class);
                            ContextCompat.startForegroundService(Home.this.getActivity(), i);
                            for (int j = 0; j < adapter.getItemCount(); j++) {
                                if (adapter1.getItem(j).isSelected(true)) {
                                     name = ((TextView) holder.itemView.findViewById(R.id.make)).getText().toString();
                                    Toast.makeText(getActivity(), "Data Inserted....." + name, Toast.LENGTH_SHORT).show();
                                } else {
                                    Toast.makeText(getActivity(), "Error", Toast.LENGTH_LONG).show();
                                }
                            }
                            SharedPreferences.Editor editor = Home.this.getActivity().getSharedPreferences("check", MODE_PRIVATE).edit();
                            editor.putString("state", "true");
                            editor.apply();
                            adapter.notifyDataSetChanged();

                            //Toast.makeText(Home.this.getContext(), "Service Started", Toast.LENGTH_SHORT).show();
                        }else{
                            holder.startsCar.setText("Start Car");
                            Intent i = new Intent(Home.this.getActivity(), MQTTService.class);
                            getActivity().stopService(i);
                            Toast.makeText(Home.this.getContext(), "Service Stoped", Toast.LENGTH_SHORT).show();
                            SharedPreferences.Editor editor =  Home.this.getActivity().getSharedPreferences("check", MODE_PRIVATE).edit();
                            editor.putString("state", "false");
                            editor.apply();
                            adapter.notifyDataSetChanged();
                        }
                    }
                });

为什么会这样?

使用此代码:

SharedPreferences prefs =  Home.this.getActivity().getSharedPreferences("check", MODE_PRIVATE);
  final String check_state = prefs.getString( "state", "default");

   if(!check_state.equals("false") {
        holder.startsCar.setText("Stop Car"); 
   }else { 
        holder.startsCar.setText("Start Car"); 
   }

  holder.startsCar.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(check_state.equals("false")){
                            holder.startsCar.setText("Stop Car");
                            Intent i = new Intent(Home.this.getActivity(), MQTTService.class);
                            ContextCompat.startForegroundService(Home.this.getActivity(), i);
                            for (int j = 0; j < adapter.getItemCount(); j++) {
                                if (adapter1.getItem(j).isSelected(true)) {
                                     name = ((TextView) holder.itemView.findViewById(R.id.make)).getText().toString();
                                    Toast.makeText(getActivity(), "Data Inserted....." + name, Toast.LENGTH_SHORT).show();
                                } else {
                                    Toast.makeText(getActivity(), "Error", Toast.LENGTH_LONG).show();
                                }
                            }
                            SharedPreferences.Editor editor = Home.this.getActivity().getSharedPreferences("check", MODE_PRIVATE).edit();
                            editor.putString("state", "true");
                            editor.apply();
                            adapter.notifyDataSetChanged();

                            //Toast.makeText(Home.this.getContext(), "Service Started", Toast.LENGTH_SHORT).show();
                        }else{
                            holder.startsCar.setText("Start Car");
                            Intent i = new Intent(Home.this.getActivity(), MQTTService.class);
                            getActivity().stopService(i);
                            Toast.makeText(Home.this.getContext(), "Service Stoped", Toast.LENGTH_SHORT).show();
                            SharedPreferences.Editor editor =  Home.this.getActivity().getSharedPreferences("check", MODE_PRIVATE).edit();
                            editor.putString("state", "false");
                            editor.apply();
                            adapter.notifyDataSetChanged();
                        }
                    }
                });

注意我在开头使用 prefs 设置 startsCar 文本的状态