Activity活动

Activity活动

在这里插入图片描述

打印MainActivity的生命周期
package com.example.demo;

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 final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.e(TAG, "onCreate");
        setTitle("第一个Activity");
    }

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

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

    @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, "onDestroy");
    }
}

启动MainActivity, 点击返回键,生命周期如下:
在这里插入图片描述
启动MainActivity, 点击Home键,再点击Demo图标,生命周期如下:
在这里插入图片描述

两个Activity的生命周期

1.新建SecondActivity
在这里插入图片描述
2.将生命周期 日志复制过来 注意重新定义TAG

package com.example.demo;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;

public class SecondActivity extends AppCompatActivity {

    private static final String TAG = "SecondActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        Log.e(TAG, "onCreate");
        setTitle("第二个Activity");
    }

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

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

    @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, "onDestroy");
    }
}

3.在MainActivity布局中添加按钮,在代码中实现点击事件

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="跳转"
        android:id="@+id/btn1"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
	private Button btn1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.e(TAG, "onCreate");
        setTitle("第一个Activity");

        btn1 = findViewById(R.id.btn1);
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this, SecondActivity.class);
                startActivity(intent);
            }
        });
    }

4.测试两个Activity的切换 输出日志如下:
在这里插入图片描述

Intent的使用
显示Intent
				Intent intent = new Intent();
                intent.setClass(MainActivity.this, SecondActivity.class);
                startActivity(intent);
隐式Intent
				Intent intent = new Intent();
                intent.setAction("I_WANT_EAT");
                startActivity(intent);

直接启动报错
在Manifest文件中 添加intent-filter

		<activity
            android:name=".SecondActivity"
            android:label="学生饭堂">
            <intent-filter>
                <action android:name="I_WANT_EAT" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

可以正常打开学生饭堂
此时可以继续添加一个后山饭堂 并添加声明如下

		<activity android:name=".ThirdActivity"
            android:label="后山饭堂">
            <intent-filter>
                <action android:name="I_WANT_EAT" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

此时点击按钮,会弹出两个饭堂让用户选择
如果用户还需要指定吃饭的类别是蛋包饭,代码如下

				Intent intent = new Intent();
                intent.setAction("I_WANT_EAT");
                intent.addCategory("DAN_BAO_FAN");
                startActivity(intent);

点击按钮,程序崩溃
此时为后山饭堂添加蛋包饭类别,代码如下:

	<activity android:name=".ThirdActivity"
            android:label="后山饭堂">
            <intent-filter>
                <action android:name="I_WANT_EAT" />

                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="DAN_BAO_FAN" />
            </intent-filter>
        </activity>

点击按钮 直接打开了后山饭堂

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值