我的问题是我想将活动页面存储在sharedpreferences上例如:1)用户单击按钮A到Main2Activity2)共享首选项商店Main2Activity3)重新启动应用程序,主页将变为Main2Activity。
这是我的编码,它没有任何错误,但是没有任何作用。
希望有人可以解释并向我展示解决方案
谢谢
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button) findViewById(R.id.colour1);
btn2 = (Button) findViewById(R.id.colour2);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
storepage(Main2Activity.class);
newpage();
}
});
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
storepage(Main3Activity.class);
thirdpage();
}
});
}
private void storepage(Class page) {
//name means give the name of sharedpreference file
//mode private means only this application can access shared preference
SharedPreferences sharedPreferences = getSharedPreferences("changepage", MODE_PRIVATE);
//create edit for shared preference file
SharedPreferences.Editor editor = sharedPreferences.edit();
//select the type of value need to inserted
editor.getClass();
//save the value
editor.apply();
}
private Class<? extends SharedPreferences> getpage() {
//name means give the name of sharedpreference file
//mode private means only this application can access shared preference
SharedPreferences sharedPreferences = getSharedPreferences("changepage", MODE_PRIVATE);
Class<? extends SharedPreferences> result = sharedPreferences.getClass();
return result;
}
private void newpage()
{
Intent intent = new Intent(this,Main2Activity.class);
startActivity(intent);
}
private void thirdpage()
{
Intent intent = new Intent(this,Main3Activity.class);
startActivity(intent);
}
}
SPLASHSCREEN类
public class SplashScreenActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
checkBtnClicked();
}
private void checkBtnClicked() {
boolean isFirstRun = getSharedPreferences("PREF", MODE_PRIVATE)
.getBoolean("isBtnClicked", false);
if (isBtnClicked) {
startActivity(new Intent(this, Main2Activity.class));
finish();
} else {
startActivity(new Intent(this, //activity where button is));
finish();
}
}
}
活动按钮在哪里
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//store boolean to sharedpreferences
}
});