27.体验活动的生命周期

xiaoxiao2021-02-28  147

1.新建ActivityLifeCycleTest项目。 2.新建两个子活动NormalActivity和DialogActivity并生成相应布局。

normal_layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.activitylifecycletest.NormalActivity"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="This is a normal activity"/> </LinearLayout>

dialog_layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.activitylifecycletest.DialogActivity"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="This is a dialog activity"/> </LinearLayout>

3.修改AndroidManifest.xml的<activity>标签配置

<activity android:name=".NormalActivity" /> <activity android:name=".DialogActivity" android:theme="@android:style/Theme.Dialog"> </activity>

4.修改activity_main.xml,定义了两个按钮,一个用于启动NormalActivity,一个用于启动DialogActivity。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.activitylifecycletest.MainActivity"> <Button android:id="@+id/start_normal_activity" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Start NormalActivity"/> <Button android:id="@+id/start_dialog_activity" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Start DialogActivity"/> </LinearLayout>

5.修改MainActivity

public class MainActivity extends AppCompatActivity { public static final String TAG = "MainActivity"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button startNormalActivity = (Button) findViewById(R.id.start_normal_activity); Button startDialogActivity = (Button) findViewById(R.id.start_dialog_activity); startNormalActivity.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this,NormalActivity.class); startActivity(intent); } }); startDialogActivity.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this,DialogActivity.class); startActivity(intent); } }); } @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"); } }

转载请注明原文地址: https://www.6miu.com/read-20623.html

最新回复(0)