Android体验活动的生命周期

木来 木来

(1)首先创建一个项目,选empty Activity即可。

(2)创建NormalActivity和dialogActivity活动

(3)在normalActivity和dialogActivity的布局里都分别添加一个文本,内容分别为normal和dialog,这里就给一个的布局

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical" android:layout_width="match_parent"    android:layout_height="match_parent">

    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:id="@+id/dialog_textview"        android:text="THIS IS A NORMAL"    />




</LinearLayout>

(4)在androidmanifest.xml中对对话框活动进行注册

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

(5)在mainActivity中写两个button布局,然后通过intent来实现交互,并且写活动的生命周期方法。

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical" android:layout_width="match_parent"    android:layout_height="match_parent">

  <Button      android:layout_width="match_parent"      android:layout_height="wrap_content"      android:id="@+id/normal_button"      android:text="点击跳转到normal活动"/>

    <Button        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:id="@+id/dialog_button"        android:text="点击跳转到dialog活动"        />




</LinearLayout>
package com.shihsf.hello;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {


    private static final String TAG = "MainActivity";

    @Override    protected void onStart() {
        super.onStart();
        Log.e(TAG,"onStart!");
    }

    @Override    protected void onResume() {
        super.onResume();
        Log.e(TAG,"onResume!");
    }


    @Override    protected void onPause() {
        super.onPause();
        Log.e(TAG,"onPause!");
    }


    @Override    protected void onStop() {
        super.onStop();
        Log.e(TAG,"onStop!");
    }


    @Override    protected void onDestroy() {
        super.onDestroy();
        Log.e(TAG,"onDestory!");
    }

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button button1=findViewById(R.id.normal_button);
        Button button2=findViewById(R.id.dialog_button);

        button1.setOnClickListener(new View.OnClickListener() {
            @Override            public void onClick(View v) {
                Intent intent=new Intent(MainActivity.this,NormalActivity.class);
                startActivity(intent);
            }
        });

        button2.setOnClickListener(new View.OnClickListener() {
            @Override            public void onClick(View v) {
                Intent intent=new Intent(MainActivity.this,DialogActivity.class);
                startActivity(intent);
            }
        });




    }
}
0 条评论