当我使用支持按钮关闭时,如何保存Activity
的方式,并且恢复它将与关闭时的方式相同。
这是我的Activity
代码:
public class Levels extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setTheme(android.R.style.Theme_NoTitleBar_Fullscreen);
setContentView(R.layout.levels);
final EditText anstext1 = (EditText) findViewById(R.id.anstext1);
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String result = anstext1.getText().toString();
if(result.equals("they"))
setContentView(R.layout.social);
else
Toast.makeText(getApplicationContext(), "Wrong", Toast.LENGTH_LONG).show();
}
});
}
}
答案 0 :(得分:3)
您将不得不实现onSavedInstanceState()并使用int填充它以显示您正在显示的视图。
然后,在你的onCreate(Bundle savedInstanceState)方法中,你将从包中挖出int并将你的内容视图设置为。
public class yourActivity extends Activity {
private static final String KEY_STATE_VIEW_ID = "view_id";
private int _viewId = R.layout.levels;
@Override
public void onCreate(Bundle savedInstanceState) {
if (savedInstanceState != null) {
if (savedInstanceState.containsKey(KEY_STATE_VIEW_ID) {
_viewId = savedInstanceState.getInt(KEY_STATE_VIEW_ID);
}
}
setContentView(_viewId);
// in your onClick set viewId to R.layout.social
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState){
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putInt(KEY_STATE_VIEW_ID, _viewId);
}
}
可在此处找到一个示例:http://www.how-to-develop-android-apps.com/tag/onsaveinstancestate/