首页 > 其他分享 >APP学习8(跳转,activity、intent意图)

APP学习8(跳转,activity、intent意图)

时间:2023-03-09 23:44:25浏览次数:29  
标签:APP activity 意图 Intent Activity intent 跳转

1. 生命周期

5个状态。

启动状态:当Activity启动之后便会进入下一状态。

运行状态:Activity处于屏幕最前端,可与用户进行交互。

暂停状态:Activity仍然可见,但是无法获取焦点,用户对他的操作没有相应。

停止状态:Activity完全不可见,系统内存不足时回销毁该Activity。

销毁状态:Activity将被清理出内存。

 

 可以用代码观察这些阶段什么时候执行。

2.Activity的创建和配置

p16

3. Intent意图

程序中各组件进行交互的一种重要方式。他可以指定当前组件要执行的动作,还可以在不同组件之间进行数据传递。

一般用于启动ActivityService以及发送广播。根据开启目标组件的方式不同,Intent分为两种类型:显示意图和隐式意图。

当发送一个隐式Intent之后,Android系统会将他与程序中的每一个组件的过滤器进行匹配,匹配的属性有action、data、category,需要这三个属性都匹配成功才能唤起相应的组件。注意:必须要添加action属性。

 两种方式的实现代码:

activity_main.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"
    >

    <Button
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:text="跳转到第二个页面"
        android:onClick="myClick1"
        >
    </Button>

    <Button
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:text="跳转到hello页面"
        android:id="@+id/btn"
        ></Button>
</LinearLayout>

activity_second.xml

<?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=".SecondActivity">

    <Button
        android:id="@+id/button"
        android:layout_width="209dp"
        android:layout_height="176dp"
        android:text="第二个页面"
        tools:layout_editor_absoluteX="101dp"
        tools:layout_editor_absoluteY="292dp"
        tools:ignore="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>

activity_hello.xml

<?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=".HelloActivity">

    <Button
        android:id="@+id/button2"
        android:layout_width="180dp"
        android:layout_height="237dp"
        android:text="Hello"
        tools:layout_editor_absoluteX="102dp"
        tools:layout_editor_absoluteY="273dp"
        tools:ignore="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java

package com.example.myapp;


import androidx.appcompat.app.AppCompatActivity;

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


public class MainActivity extends AppCompatActivity {
    private Button btn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn=findViewById(R.id.btn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //2.隐式意图:根据action属性跳转到清单文件中进行匹配,然后执行对应的activity
                Intent intent = new Intent("hello");//这里会去清单中找过滤器的action叫这个名字的
                startActivity(intent);

            }
        });
    }
    //用onClick方式
     public void myClick1(View view){
        //1.显示意图的使用方法实现页面跳转
        //第一个参数:当前页面;第二个参数:要跳转的页面
        Intent intent = new Intent(MainActivity.this,SecondActivity.class);
        //执行意图
        this.startActivity(intent);
    }
    //用匿名类方式

}

AndroidManifest.xml

清单文件

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

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/Theme.MyApp"
        tools:targetApi="31">
        <activity
            android:name=".HelloActivity"
            android:exported="false" >
            <intent-filter>
                <action android:name="hello"></action>
                <category android:name="android.intent.category.DEFAULT"></category>
            </intent-filter>

        </activity>
        <activity
            android:name=".SecondActivity"
            android:exported="false" />
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

 

标签:APP,activity,意图,Intent,Activity,intent,跳转
From: https://www.cnblogs.com/hmy22466/p/17181930.html

相关文章