简体   繁体   English

Android-每次启动活动时,SwitchCompat OnCheckedChangeListener操作都会运行

[英]Android - SwitchCompat OnCheckedChangeListener action is running every time the activity is started

I have some SwitchCompat in my Activity, I set the OnCheckedChangeListener to one of them but (using SharedPreferences ), every time I start the Activity, the action of OnCheckedChangeListener is performed regardless of whether it is on or off (Which is very very bad for performance as the On state action is to run a shell script and show a SnackBar as it takes some time). 我的Activity中有一些SwitchCompat ,我将OnCheckedChangeListener设置为其中之一,但是(使用SharedPreferences ),每次启动Activity时,无论它处于打开还是关闭状态,都会执行OnCheckedChangeListener的操作(这对用户非常不利)性能(如“ 打开”状态操作)是运行shell脚本并显示SnackBar ,这需要花费一些时间。

Here is a small piece of code... 这是一小段代码...

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
        //Private stuffs...
        SwitchCompat play; //and many others
        public static final String PREFS_NAME = "SwitchButton";

        protected void onCreate(Bundle savedInstanceState) {
        // ...
        play = (SwitchCompat) findViewById(R.id.play_switch);
        play.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    Shell.SU.run("sh /data/data/br.com.packagename/play_on");
                    Snackbar snack_play_on = Snackbar.make(play, R.string.play_on, Snackbar.LENGTH_SHORT);
                    snack_play_on.show();

                    SharedPreferences.Editor editor = getSharedPreferences("SwitchButton", MODE_PRIVATE).edit();
                    editor.putBoolean("onPlay", true);
                    editor.apply();

                } else {
                    Shell.SU.run("sh /data/data/br.com.packagename/play_off");
                    SharedPreferences.Editor editor = getSharedPreferences("SwitchButton", MODE_PRIVATE).edit();
                    editor.putBoolean("onPlay", false);
                    editor.apply();

                    Snackbar snack_play_off = Snackbar.make(play, R.string.play_off, Snackbar.LENGTH_SHORT);
                    snack_play_off.show();
                }
            }
        });
        play.setChecked(sharedPrefs.getBoolean("onPlay", false));

So... Every time a open the activity (ot the app itself) that Snackbar shows, and the linked action to the On state of the SwitchCompat runs. 所以...每次打开Snackbar显示的活动(应用程序本身),并运行与SwitchCompat的On状态关联的动作。 This causes too many frames skip when loading the Activity (arround to 230 in a 1GB, 1.2GHz quad device). 加载“活动”时,这会导致跳帧过多(在1GB,1.2GHz的四核设备中约为230个)。 There is more then one Switch, four or five. 开关多于一个,四个或五个。

What should I do? 我该怎么办? Am I missing soomething or putting the code in the wrong place? 我会丢失某些东西还是将代码放在错误的位置? Should I use other methods like OnResume, OnPause etc? 是否应该使用其他方法,例如OnResume,OnPause等?

Calling setChecked() invokes the listener they same way a user click would. 调用setChecked()会以与用户单击相同的方式调用侦听器。

I now handle setting up all checkboxes like this: 现在,我要像这样设置所有复选框:

        play = (SwitchCompat) findViewById(R.id.play_switch);
        play.setOnCheckedChangeListener(null);
        play.setChecked(sharedPrefs.getBoolean("onPlay", false));
        play.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
           ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM