我正在制作一个活动,该活动将在活动开始之前在执行任何功能之前编辑共享首选项。我被卡住了,不知道该怎么做。我的应用程序仅在我关闭应用程序或返回另一个活动时编辑共享首选项。我想在活动开始时编辑它们而不执行任何功能,即checkpreferences(); .Precisely 如何在活动开始时编辑共享偏好?
public class MyActivity extends Activity {
private SharedPreferences app_preferences;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Get the app's shared preferences
app_preferences = PreferenceManager.getDefaultSharedPreferences(this);
final int id = 42;
int idd = app_preferences.getInt("idd", 0);
// I am Stuck in this part
SharedPreferences.Editor editor = app_preferences.edit();
editor.putInt("idd", 43);
editor.commit(); // Very important
final int iddd = idd;
checkpreferences(id, iddd);
}
private void checkpreferences(int di, int dii) {
if(di == dii) {
Toast.makeText(AudioActivity.this,"id is same"+dii+"="+di , Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(AudioActivity.this,"id is different"+dii+"!="+di, Toast.LENGTH_SHORT).show();
}
}
}