我正在教自己如何使用Fragments和SharedPreferences。我正在创建一个使用导航抽屉在片段之间进行切换的应用。
在片段A中,我有8个切换按钮。当用户为B留下片段A然后返回时,我需要切换为保持这种状态的切换按钮,直到用户手动重置为止。切换和重置工作正常,但是我在多次尝试使SharedPref保持按钮状态时没有成功。
我认为这可能是个问题,因为每次选择菜单项时它都会调用“new”。如果是这样,我该如何解决它?
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch(item.getItemId()){
case R.id.nav_carb:
getSupportFragmentManager().beginTransaction().replace(R.id.fragmentContainer,
new CarbFragment()).commit();
break;
MainActivity :
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private DrawerLayout drawer;
ToggleButton toggleButton, toggleButton1, toggleButton2, toggleButton3, toggleButton4, toggleButton5, toggleButton6, toggleButton7;
boolean selected, tb;
SharedPreferences preferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
android.support.v7.widget.Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView =findViewById(R.id.navView);
navigationView.setNavigationItemSelectedListener(this);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
preferences = getSharedPreferences("checkedState", Context.MODE_PRIVATE);
if(preferences.contains("tbpref")){
selected = preferences.getBoolean("tbpref", true);
}
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch(item.getItemId()){
case R.id.nav_carb:
getSupportFragmentManager().beginTransaction().replace(R.id.fragmentContainer,
new CarbFragment()).commit();
break;
case R.id.nav_cal:
getSupportFragmentManager().beginTransaction().replace(R.id.fragmentContainer,
new CalFragment()).commit();
break;
}
drawer.closeDrawer(GravityCompat.START);
return true;
}
FragmentA
public class CarbFragment extends Fragment {
ToggleButton toggleButton, toggleButton1, toggleButton2, toggleButton3, toggleButton4, toggleButton5, toggleButton6, toggleButton7;
Button reset;
boolean selected, tb;
SharedPreferences preferences;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
ConstraintLayout rootView = (ConstraintLayout) inflater.inflate(R.layout.fragment_carb_count, container, false);
toggleButton = rootView.findViewById(R.id.toggleButton);
toggleButton1 = rootView.findViewById(R.id.toggleButton1);
toggleButton2 = rootView.findViewById(R.id.toggleButton2);
toggleButton3 = rootView.findViewById(R.id.toggleButton3);
toggleButton4 = rootView.findViewById(R.id.toggleButton4);
toggleButton5 = rootView.findViewById(R.id.toggleButton5);
toggleButton6 = rootView.findViewById(R.id.toggleButton6);
toggleButton7 = rootView.findViewById(R.id.toggleButton7);
reset = rootView.findViewById(R.id.button);
reset.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
toggleButton.setChecked(false);
toggleButton.setBackgroundColor(Color.rgb(255, 153, 153));
toggleButton1.setChecked(false);
toggleButton1.setBackgroundColor(Color.rgb(255, 219, 153));
toggleButton2.setChecked(false);
toggleButton2.setBackgroundColor(Color.rgb(255, 255, 153));
toggleButton3.setChecked(false);
toggleButton3.setBackgroundColor(Color.rgb(153, 255, 153));
toggleButton4.setChecked(false);
toggleButton4.setBackgroundColor(Color.rgb(153, 153, 255));
toggleButton5.setChecked(false);
toggleButton5.setBackgroundColor(Color.rgb(255, 153, 255));
toggleButton6.setChecked(false);
toggleButton6.setBackgroundColor(Color.rgb(255, 255, 255));
toggleButton7.setChecked(false);
toggleButton7.setBackgroundColor(Color.rgb(204, 204, 204));
Toast toast = Toast.makeText(getContext(), "Ready to go again!", Toast.LENGTH_SHORT);
toast.show();
}
});
toggleButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
toggleButton.setBackgroundColor(Color.DKGRAY);
Toast toast = Toast.makeText(getContext(), "Nom Nom Nom", Toast.LENGTH_SHORT);
toast.show();
if (toggleButton.isChecked()) {
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("tbpref", true);
editor.apply();
}
else{
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("tbpref", false);
editor.apply();
}
}
});
toggleButton1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
toggleButton1.setBackgroundColor(Color.DKGRAY);
Toast toast = Toast.makeText(getContext(), "You've got this!", Toast.LENGTH_SHORT);
toast.show();
}
});
toggleButton2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
toggleButton2.setBackgroundColor(Color.DKGRAY);
Toast toast = Toast.makeText(getContext(), "Look at you go!!!", Toast.LENGTH_SHORT);
toast.show();
}
});
toggleButton3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
toggleButton3.setBackgroundColor(Color.DKGRAY);
Toast toast = Toast.makeText(getContext(), "You are crushing this!", Toast.LENGTH_SHORT);
toast.show();
}
});
toggleButton4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
toggleButton4.setBackgroundColor(Color.DKGRAY);
Toast toast = Toast.makeText(getContext(), "Killin' IT!", Toast.LENGTH_SHORT);
toast.show();
}
});
toggleButton5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
toggleButton5.setBackgroundColor(Color.DKGRAY);
Toast toast = Toast.makeText(getContext(), "Carbs don't control you!", Toast.LENGTH_SHORT);
toast.show();
}
});
toggleButton6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
toggleButton6.setBackgroundColor(Color.DKGRAY);
Toast toast = Toast.makeText(getContext(), "You're so awesome", Toast.LENGTH_SHORT);
toast.show();
}
});
toggleButton7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
toggleButton7.setBackgroundColor(Color.DKGRAY);
Toast toast = Toast.makeText(getContext(), "Kickin' butt and taking names", Toast.LENGTH_SHORT);
toast.show();
}
});
return rootView;
}
}
非常感谢能提供的任何帮助。我在这里看了几个答案,但似乎没有一个适合我的问题。我试着实现一些答案,但它们似乎也不适合我。谢谢。
答案 0 :(得分:1)
您只能将状态写入共享首选项,而不是在片段内从它们中读取它。为了实现这一目标,您必须在onCreateView
方法中执行以下操作:
boolean isChecked = preferences.getBoolean("tbpref");
toggleButton.setChecked(isChecked);