Activity生命周期
规划图:
XML:1.1代码
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="提示"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="俺是一个Activity形式的对话框"
android:textSize="30sp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="确定"
/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="取消"
/>
</LinearLayout>
XML:2.代码
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="跳B"
android:onClick="jumpB"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="弹出对话框"
android:onClick="showDialog"
/>
JAVA:代码1.2
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i("test","Main-->onCreate");
}
@Override
protected void onStart() {
super.onStart();
Log.i("test","Main-->onStart");
}
@Override
protected void onResume() {
super.onResume();
Log.i("test","Main-->onResume");
}
@Override
protected void onPause() {
super.onPause();
Log.i("test","Main-->onPause");
}
@Override
protected void onStop() {
super.onStop();
Log.i("test","Main-->onStop");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.i("test","Main-->onDestroy");
}
@Override
protected void onRestart() {
super.onRestart();
Log.i("test","Main-->onRestart");
}
public void jumpB(View view){
Intent intent=new Intent(this,BActivity.class);
startActivity(intent);
}
//弹出对话框
public void showDialog(View view){
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setTitle("提示");
builder.setMessage("您确定要离开俺吗?");
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
builder.setNeutralButton("取消",null);
builder.show();
}
}
JAVA:代码2.2
public class BActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_b);
Log.i("test","B-->onCreate");
}
@Override
protected void onStart() {
super.onStart();
Log.i("test","B-->onStart");
}
@Override
protected void onResume() {
super.onResume();
Log.i("test","B-->onResume");
}
@Override
protected void onPause() {
super.onPause();
Log.i("test","B-->onPause");
}
@Override
protected void onStop() {
super.onStop();
Log.i("test","B-->onStop");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.i("test","B-->onDestroy");
}
@Override
protected void onRestart() {
super.onRestart();
Log.i("test","B-->onRestart");
}
}