1. 程式人生 > >體驗android Activity的完整生命週期

體驗android Activity的完整生命週期

建立Activity_1活動和Activity_2活動

Activity_1:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.lhc.mywebview.Activity_1"> <TextView android:id=
"@+id/textview" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:text="這是第一個活動" android:textSize="20sp" android:textColor="#000000"/> </RelativeLayout>
public class Activity_1 extends Activity {
    private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_1); textView = (TextView) findViewById(R.id.textview); } }
Activity_2:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.lhc.mywebview.Activity_2">
    <TextView
android:id="@+id/textview2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#000000"
android:textSize="20sp"
android:text="這是第二個活動"
android:gravity="center_horizontal"/>
</RelativeLayout>
public class Activity_2 extends Activity {
    private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
textView = (TextView) findViewById(R.id.textview2);
}
}

在MainActivity的xml檔案中設定按鈕:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.lhc.mywebview.MainActivity"
android:orientation="vertical">
    <Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="活動一"/>
    <Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="活動二"/>
</LinearLayout>
MainActivity的生命週期(通過logcat檢視):
public class MainActivity extends Activity {
    private Button button1,button2;
    public static final String TAG = "Mainactivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
Log.d(TAG, "onCreate已建立 ");
setContentView(R.layout.activity_main);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button1.setOnClickListener(new View.OnClickListener() {
            @Override
public void onClick(View v) {
                Intent intent1 = new Intent(MainActivity.this,Activity_1.class);
startActivity(intent1);
}
        });
button2.setOnClickListener(new View.OnClickListener() {
            @Override
public void onClick(View v) {
                Intent intent2 = new Intent(MainActivity.this,Activity_2.class);
startActivity(intent2);
}
        });
}

    @Override
protected void onStart() {
        super.onStart();
Log.d(TAG, "onStart:建立 ");
}

    @Override
protected void onResume() {
        super.onResume();
Log.d(TAG, "onResume:建立 ");
}

    @Override
protected void onPause() {
        super.onPause();
Log.d(TAG, "onPause: 建立");
}

    @Override
protected void onStop() {
        super.onStop();
Log.d(TAG, "onStop:建立 ");
}

    @Override
protected void onDestroy() {
        super.onDestroy();
Log.d(TAG, "onDestroy:建立 ");
}

    @Override
protected void onRestart() {
        super.onRestart();
Log.d(TAG, "onRestart: 建立");
}
}