Android 测试生命周期的APP

mac2022-06-30  84

三个布局文件:

activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center_vertical" > <TextView android:id="@+id/text_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="主Activty" /> <Button android:id="@+id/start_normal_activity" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="启动普通的Activity" /> <Button android:id="@+id/start_dialog_activity" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_gravity="center_horizontal" android:text="启动对话式的Activity" /> </LinearLayout> normal_layout.xml:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="普通的Activity" /> </LinearLayout> dialog_layout.xml:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_gravity="center_vertical" android:text="对话框式的Activity" /> </LinearLayout> 三个Activity,其他两个不用添加代码

public class MainActivity extends Activity { public static final String TAG = "MainActivity"; Button startNormalActivity; Button startDialogActivity; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.v(TAG, "onCreate"); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); startNormalActivity = (Button) findViewById(R.id.start_normal_activity); startDialogActivity = (Button) findViewById(R.id.start_dialog_activity); startNormalActivity.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this, NormalActivity.class); startActivity(intent); } }); startDialogActivity.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this, DialogActivity.class); startActivity(intent); } }); } @Override protected void onStart() { super.onStart(); Log.v(TAG, "onStart"); } @Override protected void onResume() { super.onResume(); Log.v(TAG, "onResume"); } @Override protected void onPause() { super.onPause(); Log.v(TAG, "onPause"); } @Override protected void onStop() { super.onStop(); Log.v(TAG, "onStop"); } @Override protected void onDestroy() { super.onDestroy(); Log.v(TAG, "onDestroy"); } @Override protected void onRestart() { super.onRestart(); Log.v(TAG, "onRestart"); } }

打开Eclipse启动APP,在LogCat输入tag:MainActivity,然后操作APP即可看到生命周期的变化。

如下图:

版权声明:本文为博主原创文章,未经博主允许不得转载。

转载于:https://www.cnblogs.com/kinglearnjava/p/4883262.html

最新回复(0)