如果在一个会话中每个用户只能有一个配置文件(如您上面建议的那样),为什么不使用Java singleton class
.
private ProfileOject() {
// Exists only to defeat instantiation.
}
public static ProfileObject getInstance() {
if(instance == null) {
instance = new ProfilerObject();
}
return instance;
}
现在像往常一样使用 getter 和 setter。
在每项活动中,您都可以获得Profile
如下信息:
profile = ProfileObject.getInstance();
这意味着如果您在 中进行更新Fragment A
,当activity
调用 时,它将从 中获取更新后的值profile object
。
关于onRotate/Pause/Resume
使用savedInstanceState
,请参见下面的示例:
//Use onSaveInstanceState(Bundle) and onRestoreInstanceState
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
// Save UI state changes to the savedInstanceState.
// This bundle will be passed to onCreate if the process is
// killed and restarted.
savedInstanceState.putBoolean("MyBoolean", true);
// etc.
super.onSaveInstanceState(savedInstanceState);
}
//onRestoreInstanceState
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// Restore UI state from the savedInstanceState.
// This bundle has also been passed to onCreate.
boolean myBoolean = savedInstanceState.getBoolean("MyBoolean");
}