如何手动调用 onSaveInstanceState()
How to manually call onSaveInstanceState()
我正在编写一个程序,我在同一个 activity 上调用多个布局,但后来我注意到,当我切换布局时,切换前所做的更改不会恢复并且 onSavedInstanceState(Bundle outState)
不会叫。我试图手动调用该方法,但我无法获得 Bundle outState
.
所以真正的问题是:如何获取和存储 activity 的当前状态以便在我选择的时间恢复 and/or?
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contact_view);
// more code
}
@Override
public void onBackPressed() {
if (layoutId == R.layout.activity_contact_view) exit();
else if (layoutId == R.layout.main) {
Toast.makeText(NsdChatActivity.this, "Successful back button action", Toast.LENGTH_SHORT).show();
setContentView(R.layout.activity_contact_view);
refreshContactList();
}
}
然后从一个单独的 class
public void updateList(final int found) {
LinearLayout layxout = (LinearLayout) ((Activity)mContext).getWindow().getDecorView().findViewById(R.id.others);
TextView t = new TextView(mContext);
t.setClickable(true);
t.setText(found + ". " + activity.sNames.get(found));
t.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
t.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//show chat view
activity.setContentView(R.layout.main);
TextView name = (TextView)activity.findViewById(R.id.clientName);
name.setText(activity.sNames.get(found).split(" \(")[0]);
final ScrollView scroll = (ScrollView)activity.findViewById(R.id.scroll);
scroll.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean b) {
scroll.fullScroll(View.FOCUS_DOWN);
}
});
}
});
layxout.addView(t);
}
也许你应该看看 Application Fundamentals
Android calls onSaveInstanceState() before the activity becomes vulnerable to being destroyed by the system, but does not bother calling it when the instance is actually being destroyed by a user action (such as pressing the BACK key)
所以你在同一个activity上调用多个布局可能不会造成上述情况。更详细的可以参考问题Android: Saving a state during Android lifecycle。希望对您有所帮助!
我可能会迟到,但你能做的就是保持你作为 class 成员的状态。这样你就可以随时恢复状态。
Bundle mState;
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
// Save the state
savedInstanceState = mState;
super.onSaveInstanceState(savedInstanceState);
}
@Overrite
public void onRestoreInstanceState(Bundle savedInstance){
mState = savedInstance;
Restore();
}
public void Restore(){
//access your state and restore
}
此外,您不应该使用 setContentView 在视图之间切换,这是一种昂贵的方式。您可能想要检查 ViewSwitcher or ViewFlipper 或以某种方式实现 Fragments。
我正在编写一个程序,我在同一个 activity 上调用多个布局,但后来我注意到,当我切换布局时,切换前所做的更改不会恢复并且 onSavedInstanceState(Bundle outState)
不会叫。我试图手动调用该方法,但我无法获得 Bundle outState
.
所以真正的问题是:如何获取和存储 activity 的当前状态以便在我选择的时间恢复 and/or?
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contact_view);
// more code
}
@Override
public void onBackPressed() {
if (layoutId == R.layout.activity_contact_view) exit();
else if (layoutId == R.layout.main) {
Toast.makeText(NsdChatActivity.this, "Successful back button action", Toast.LENGTH_SHORT).show();
setContentView(R.layout.activity_contact_view);
refreshContactList();
}
}
然后从一个单独的 class
public void updateList(final int found) {
LinearLayout layxout = (LinearLayout) ((Activity)mContext).getWindow().getDecorView().findViewById(R.id.others);
TextView t = new TextView(mContext);
t.setClickable(true);
t.setText(found + ". " + activity.sNames.get(found));
t.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
t.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//show chat view
activity.setContentView(R.layout.main);
TextView name = (TextView)activity.findViewById(R.id.clientName);
name.setText(activity.sNames.get(found).split(" \(")[0]);
final ScrollView scroll = (ScrollView)activity.findViewById(R.id.scroll);
scroll.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean b) {
scroll.fullScroll(View.FOCUS_DOWN);
}
});
}
});
layxout.addView(t);
}
也许你应该看看 Application Fundamentals
Android calls onSaveInstanceState() before the activity becomes vulnerable to being destroyed by the system, but does not bother calling it when the instance is actually being destroyed by a user action (such as pressing the BACK key)
所以你在同一个activity上调用多个布局可能不会造成上述情况。更详细的可以参考问题Android: Saving a state during Android lifecycle。希望对您有所帮助!
我可能会迟到,但你能做的就是保持你作为 class 成员的状态。这样你就可以随时恢复状态。
Bundle mState;
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
// Save the state
savedInstanceState = mState;
super.onSaveInstanceState(savedInstanceState);
}
@Overrite
public void onRestoreInstanceState(Bundle savedInstance){
mState = savedInstance;
Restore();
}
public void Restore(){
//access your state and restore
}
此外,您不应该使用 setContentView 在视图之间切换,这是一种昂贵的方式。您可能想要检查 ViewSwitcher or ViewFlipper 或以某种方式实现 Fragments。