1.调用
1 //暂停 2 @Override 3 protected void onPause() { 4 super.onPause(); 5 Log.e("TAG", "onPause()被调用"); 6 } 7 //重新启动 8 @Override 9 protected void onRestart() { 10 super.onRestart(); 11 Log.e("TAG", "onRestart()被调用"); 12 } 13 //启动 14 @Override 15 protected void onStart() { 16 super.onStart(); 17 Log.e("TAG", "onStart()被调用"); 18 } 19 //停止 20 @Override 21 protected void onStop() { 22 super.onStop(); 23 Log.e("TAG", "onStop()被调用"); 24 } 25 //销毁 26 @Override 27 protected void onDestroy() { 28 super.onDestroy(); 29 Log.e("TAG", "onDestroy()被调用"); 30 } 31 //继续 32 @Override 33 protected void onResume() { 34 super.onResume(); 35 Log.e("TAG", "onResume()被调用"); 36 }
运行情况如下:
[博主笔记本电脑内存容量略低,只能用真机运行]
【一、这是真机刚运行时的日志反馈】
【二、旋转屏幕时的日志反馈】
2、显示提示信息
【JAVA类中添加代码】
1 //点击事件方法 2 public void bt_onclick(View v) 3 { 4 //显示提示信息 5 //方法链 6 // Toast toast = Toast.makeText(); 7 // toast.show(); 8 Toast.makeText(TestActivity1.this, "消息发送成功", Toast.LENGTH_LONG).show(); 9 } 10 11 public void close_onclick(View v) 12 { 13 //关闭activity 14 finish(); 15 }
【添加在xml文件中的代码】
1 <Button 2 android:layout_width="0dp" 3 android:layout_height="wrap_content" 4 android:layout_weight="1" 5 android:text="取消发送" 6 android:textStyle="bold" 7 android:onClick="close_onclick" 8 /> 9 <Button 10 android:layout_width="0dp" 11 android:layout_height="wrap_content" 12 android:layout_weight="1" 13 android:text="发送短信" 14 android:textStyle="bold" 15 android:onClick="bt_onclick" 16 />
(输入Toast后在出现的提示框中选择没有图标的。)
3.状态的保存应用
【日志输出】
1 //保存状态 2 @Override 3 public void onSaveInstanceState(Bundle outState) { 4 super.onSaveInstanceState(outState); 5 Log.e("TAG", "保存应用状态"); 6 7 outState.putString("et1",et1.getText().toString()); 8 outState.putString("et2",et2.getText().toString()); 9 outState.putString("et3",et3.getText().toString()); 10 } 11 //恢复状态 12 @Override 13 protected void onRestoreInstanceState(Bundle savedInstanceState) { 14 super.onRestoreInstanceState(savedInstanceState); 15 Log.e("TAG", "恢复应用状态"); 16 17 et1.setText(savedInstanceState.getString("et1")); 18 et2.setText(savedInstanceState.getString("et2")); 19 et3.setText(savedInstanceState.getString("et3")); 20 }
日志输出内容:
1 //成员变量 2 EditText et1; 3 EditText et2; 4 EditText et3; 5 et1 = (EditText)findViewById(R.id.et1); 6 et2 = (EditText)findViewById(R.id.et2); 7 et3 = (EditText)findViewById(R.id.et3); 8 9 Log.e("TAG", "oncreate()被调用"); 10 11 if (savedInstanceState != null && !savedInstanceState.isEmpty()){ 12 et1.setText(savedInstanceState.getString("et1")+"恢复之后的"); 13 et2.setText(savedInstanceState.getString("et2")+"恢复之后的"); 14 et3.setText(savedInstanceState.getString("et3")+"恢复之后的"); 15 }