public class MainActivity extends Activity {
private int xueLiang;
private Button button;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.ac_loign_but);
if (savedInstanceState == null) {
xueLiang = 1000;
Toast.makeText(MainActivity.this, "初始血量为" + xueLiang , 0).show();
}else {
xueLiang = savedInstanceState.getInt("xueLiang");
Toast.makeText(MainActivity.this, "应用出现意外,初始血量为" + xueLiang , 0).show();
}
xueLiang = savedInstanceState == null?1000:savedInstanceState.getInt("xueLiang");
Log.i("myTag", "onCreate---------");
}
public void loginBtu(View v) {
Intent intent = new Intent(MainActivity.this,LoginJump.class);
startActivity(intent);
}
protected void onDestroy() {
super.onDestroy();
Log.i("myTag", "onDestroy-----------");
}
protected void onStart() {
super.onStart();
xueLiang = xueLiang -50;
Log.i("myTag", "onStart-----------");
Toast.makeText(MainActivity.this, "开始打怪,血量为" + xueLiang, 0).show();
}
protected void onStop() {
super.onStop();
Log.i("myTag", "onStop-----------");
}
protected void onResume() {
super.onResume();
xueLiang = xueLiang - 100;
Log.i("myTag", "onResume-----------");
Toast.makeText(MainActivity.this, "开始打boss,血量为" + xueLiang, 0).show();
}
protected void onPause() {
super.onPause();
Log.i("myTag", "onPauses-----------");
}
protected void onRestart() {
super.onRestart();
Log.i("myTag", "onRestart-----------");
}
/**
* 当应用意外死亡是时(内存不足,屏幕切换等),此方法会自动调用,自动保存当前视图的状态
*
* 我们可以利用此方法进行数据保存
*
* Bundle outState 会被传递到下一次打开activity的oncreate方法中
* */
protected void onSaveInstanceState(Bundle outState) {
// TODO Auto-generated method stub
super.onSaveInstanceState(outState);
//通过键值对的形式,将数据保存下来
outState.putInt("xueLiang", xueLiang);
Toast.makeText(MainActivity.this, "应用出现意外,保存了数据", 0).show();
}
}
横竖屏切换保存数据
最新推荐文章于 2022-10-24 09:00:00 发布