Android Studio新手求助:关闭Activity后如何保存音乐开关状态
解决Settings Activity状态持久化的问题
嗨,我来帮你搞定这个状态保存的问题~首先得明确:savedInstanceState机制并不适合你这个场景,它主要用来处理Activity被系统临时销毁(比如屏幕旋转、内存不足被回收)时的临时状态保存,当你主动关闭Activity后,这个Bundle里的数据就会被丢弃,没法用来持久化保存音乐开关的状态。
你需要用SharedPreferences来做持久化存储,这是Android里专门用来保存轻量级键值对数据的工具,非常适合保存这种开关类的状态。下面是具体的修改步骤和代码调整:
步骤说明
- 初始化SharedPreferences实例,用来读写状态
- 在Activity启动时,读取之前保存的状态,恢复图标和音乐服务的状态
- 在切换音乐开关时,同步把状态保存到SharedPreferences里
修改后的代码
package com.example.mainmenu; import android.content.Intent; import android.content.SharedPreferences; import android.graphics.Color; import android.graphics.drawable.ColorDrawable; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.DisplayMetrics; import android.view.MotionEvent; import android.view.View; import android.view.WindowManager; import android.widget.ImageView; public class settings extends AppCompatActivity { private ImageView close; private ImageView help; // 定义SharedPreferences和对应的键名 private SharedPreferences soundPrefs; private static final String SOUND_ENABLED_KEY = "sound_enabled"; // 把开关状态变量提到类级别,方便全局访问 private boolean soundEnabled = true; protected void onCreate(final Bundle savedInstanceState) { getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); super.onCreate(savedInstanceState); setContentView(R.layout.custompopup); DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); final ImageView sound; close =(ImageView) findViewById(R.id.txtclose); sound = (ImageView) findViewById(R.id.button_sound); help = (ImageView) findViewById(R.id.button_help); // 初始化SharedPreferences soundPrefs = getSharedPreferences("AppSettings", MODE_PRIVATE); // 读取之前保存的状态,默认值为true(开启音乐) soundEnabled = soundPrefs.getBoolean(SOUND_ENABLED_KEY, true); // 根据保存的状态初始化图标和服务 updateSoundState(sound); help.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent secondactivtyintent = new Intent(settings.this, panuto1.class); startActivity(secondactivtyintent); } }); help.setOnTouchListener(new View.OnTouchListener(){ public boolean onTouch(View v, MotionEvent event) { switch(event.getAction()) { case MotionEvent.ACTION_DOWN : help.setImageResource(R.drawable.help); break; case MotionEvent.ACTION_UP : help.setImageResource(R.drawable.help1); break; } return false; } }); close.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { moveTaskToBack(false); finish(); } }); sound.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // 切换状态 soundEnabled = !soundEnabled; // 更新图标和服务 updateSoundState(sound); // 保存状态到SharedPreferences SharedPreferences.Editor editor = soundPrefs.edit(); editor.putBoolean(SOUND_ENABLED_KEY, soundEnabled); editor.apply(); } }); } // 抽取统一的状态更新方法,避免重复代码 private void updateSoundState(ImageView sound) { if(soundEnabled) { sound.setImageResource(R.drawable.sound); startService(new Intent(settings.this, BackgroundSoundService.class)); // 设置触摸反馈 sound.setOnTouchListener(new View.OnTouchListener(){ public boolean onTouch(View v, MotionEvent event) { switch(event.getAction()) { case MotionEvent.ACTION_DOWN : sound.setImageResource(R.drawable.sound1); break; case MotionEvent.ACTION_UP : sound.setImageResource(R.drawable.sound); break; } return false; } }); } else { sound.setImageResource(R.drawable.nosound); stopService(new Intent(settings.this, BackgroundSoundService.class)); // 设置触摸反馈 sound.setOnTouchListener(new View.OnTouchListener(){ public boolean onTouch(View v, MotionEvent event) { switch(event.getAction()) { case MotionEvent.ACTION_DOWN : sound.setImageResource(R.drawable.nosound1); break; case MotionEvent.ACTION_UP : sound.setImageResource(R.drawable.nosound); break; } return false; } }); } } }
关键修改点说明
- 把原来的
fun变量提到类级别,改名为soundEnabled,方便在整个Activity里访问 - 添加了
SharedPreferences的初始化,用getSharedPreferences创建了一个名为AppSettings的存储文件 - 在
onCreate里读取保存的状态,调用updateSoundState方法初始化图标和服务 - 把原来重复的图标切换和服务控制代码抽取到
updateSoundState方法里,减少冗余 - 在点击切换状态时,同步把新状态保存到SharedPreferences(用
editor.apply()异步保存,不会阻塞主线程)
这样修改后,即使你关闭Settings Activity,下次再打开时,SharedPreferences里保存的状态会被读取,图标和音乐服务都会恢复到之前的状态啦~
内容的提问来源于stack exchange,提问作者jane
相关产品推荐
相关产品推荐

