03activity的生命周期

NormalActivity.java

package com.lobster.activityrecycletest;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class NormalActivity extends AppCompatActivity {

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

activity_normal.xml

<?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_normal"
    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.lobster.activityrecycletest.NormalActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="this is a normal activity"
        android:textSize="18sp"
        android:textColor="#FFCCCC"/>
</RelativeLayout>

DialogActivity.java

package com.lobster.activityrecycletest;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class DialogActivity extends AppCompatActivity {

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

activity_dialog.xml

<?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_dialog"
    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.lobster.activityrecycletest.DialogActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="this is  the  dialog  activity"
        android:textSize="18sp"
        android:textColor="#FFCCCC"/>
</RelativeLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.lobster.activityrecycletest">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".NormalActivity" />
        <!--android:theme="@style/Theme.AppCompat.Dialog"将该activity设置为对话框-->
        <activity android:name=".DialogActivity"
            android:theme="@style/Theme.AppCompat.Dialog"></activity>
    </application>

</manifest>

MainActivity.java

package com.lobster.activityrecycletest;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.SectionIndexer;

public class MainActivity extends AppCompatActivity {
    //两个按钮的组件
    private View btnStartNormalActivity;
    private View btnStartDialogActivity;
    //定义一个标签,在打印日志的时候标明是哪个activity的
    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化界面的方法
        initView();
        Log.d(TAG,"onCreate()...");
    }

    //开始状态
    @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 onRestart() {
        super.onRestart();
        Log.d(TAG,"onRestart()...");
    }

    //销毁状态
    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d(TAG,"onDestroy()...");
    }

    //初始化界面
    public void initView(){
          btnStartNormalActivity = findViewById(R.id.btn_start_normal_activity);
         btnStartDialogActivity = findViewById(R.id.btn_start_dialog_activity);


        //安装btnStartNormalActivity按钮的监听器
        btnStartNormalActivity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, NormalActivity.class);
                startActivity(intent);
            }
        });
        //安装bbtnStartDialogActivity按钮的监听器
        btnStartDialogActivity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,DialogActivity.class);
                startActivity(intent);

            }
        });

    }
}

activity_main.xml

<?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_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.lobster.activityrecycletest.MainActivity">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btn_start_normal_activity"
        android:text="StartNormalActivity"
        android:background="#FFCCFF"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btn_start_dialog_activity"
        android:text="StartDialogActivity"
        android:background="#CCFFFF"
        android:layout_below="@+id/btn_start_normal_activity"
        android:layout_alignParentStart="true" />
</RelativeLayout>

posted @ 2020-10-27 13:18  L小龙虾  阅读(86)  评论(0编辑  收藏  举报