Activity 之间互相跳转

本文详细介绍了Android应用中界面跳转的方法,包括显示Intent和隐式Intent的使用,以及如何在不同Activity间传递数据和控制页面生命周期。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

概括:

界面跳转通过Intent进行跳转:
分为隐式Intent 显示Intent

一显示Intent  Actiuvity类名方式
Intent i=new Intent(MainActivity.this,Aty1.class);
startActivity(intent);
二.隐式Intent 
需要在AndroidManifest.xml文件中配置:action  category  
action名可以随便定义, category可以默认也可以自己定义,默认的是"android.intent.category.DEFAULT"通过Intent发送可以不用定义category,若是自己命名的话需要通过intent.addCategory();
1. category默认
                Intent intent=new Intent("TART");
                startActivity(intent);
AndroidManifest.xml
            <intent-filter>
                <action android:name="TART"/>
                <category android:name="android.intent.category.DEFAULT"/>  //这种名字是系统定义
            </intent-filter>
2. category自己定义
                Intent intent=new Intent("TART");
                intent.addCategory(“xxx”)
                startActivity(intent);
AndroidManifest.xml               
               <intent-filter>
                <action android:name="TART"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <category android:name="xxx"/>    //自己定义
            </intent-filter>
Button设定规则:
			定义 查找 设置监听模式
Intent设定规则:
			定义 绑定另一个页面 启动另一个页面
关闭页面:
			finish();

一. 创建MainActivity界面(内部有Button控件),创建Aty1界面(内部有Button控件),创建layout.xml文件,通过点击Button控件,在两个界面之间跳转.
在这里插入图片描述
MainActivity文件
1.在MainActivity文件里面定义Activity的七种状态:
onStart() onPause() onResume() onStop() onDestroy() onRestart()
2.定义private Button btnStartAty1;,设定点击监听状态
3.定义 Intent 绑定本界面和Aty1界面
4.点击按钮后 startActivity(i);显示Aty1界面。

package com.example.myapplication;

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 btnStartAty1;            //定义button

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);//将activity_main.xml界面绑定

        //按钮btnStartAty1
        btnStartAty1=(Button)findViewById(R.id.btnStartAty1);
        			//查找activity_main.xml 中id=btnStartAty1的组件  组件需要强制转换(Button类是View类的子类)
        btnStartAty1.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {   //点击按钮后会调用

                Intent i=new Intent(MainActivity.this,Aty1.class);//MainActivity  Aty1.class
                startActivity(i);   //启动Aty1界面
            }
        });//给钮btnStartAty1安装事件监听器
        System.out.println("MainActivity :----onCreate----");
    }

    protected void onStart() {
        super.onStart();
        System.out.println("MainActivity :----onStart----");
    }
    protected void onPause() {
        super.onPause();
        System.out.println("MainActivity :----onPause----");
    }
    protected void onResume() {

        super.onResume();
        System.out.println("MainActivity :----onResume----");
    }
    protected void onStop() {

        super.onStop();
        System.out.println("MainActivity :----onStop----");
    }
    protected void onDestroy() {
        super.onDestroy();
        System.out.println("MainActivity :----onDestroy----");
    }
    protected void onRestart() {
        super.onRestart();
        System.out.println("MainActivity :----onRestart----");
    }

}

Aty1文件
1.定义private Button btnClose; 用来跳转到MainActivity界面,
2.当点击按钮后使用 finish();关闭自己

package com.example.myapplication;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class Aty1 extends Activity {


    private Button btnClose;    //定义
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout);//将layout 见面添加进来


        btnClose=(Button)findViewById(R.id.btnClose);//在layout.xml 界面中查找id为btnClose的组件
        btnClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {//当点击

                finish();//关闭自己
            }
        });
    }


}

activity_main.xml文件
定义 <TextView <Button 两个组件,

<?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">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="你好!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/btnStartAty1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="启动Aty1"
        tools:layout_editor_absoluteX="47dp"
        tools:layout_editor_absoluteY="101dp" />

</androidx.constraintlayout.widget.ConstraintLayout>

layout.xml文件
定义 <TextView <Button组件

<?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">

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Aty1界面" />

    <Button
        android:id="@+id/btnClose"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="关闭自己" />
</LinearLayout>

里面定义activity Aty1

AndroidManifest.xml文件

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        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="Aty1"></activity>
    </application>

</manifest>

运行效果:
在这里插入图片描述
在这里插入图片描述

### Android Studio 中 Activity 之间跳转实现 在 Android 开发中,`Intent` 是用于启动新 `Activity` 的核心工具之一。通过创建显式的 `Intent` 并调用 `startActivity()` 方法,可以从当前的 `Activity` 启动另一个目标 `Activity`。 以下是具体的实现方式: #### 使用显式 Intent 进行 Activity 跳转 当需要从一个 `Activity` 跳转到另一个已知的目标 `Activity` 时,可以通过以下代码实现: ```java // 创建一个新的 Intent 对象,指定源 Activity 和目标 Activity Intent intent = new Intent(CurrentActivity.this, TargetActivity.class); // 启动目标 Activity startActivity(intent); ``` 上述代码片段展示了如何利用显式 `Intent` 来完成两个 `Activity` 间的跳转[^1]。 --- #### 带参数的数据传递 如果希望在跳转过程中携带数据,则可以使用 `putExtra()` 方法向 `Intent` 添加键值对形式的数据。接收方可以在其生命周期回调函数(如 `onCreate()` 或 `onStart()`)中提取这些数据。 发送端设置数据的方式如下所示: ```java // 定义要传递的数据 String message = "Hello from CurrentActivity"; // 将数据附加至 Intent intent.putExtra("key_message", message); // 执行跳转动作 startActivity(intent); ``` 接收端获取数据的过程则需借助于 `getIntent()` 方法以及对应的取值逻辑: ```java // 获取传入的 Intent 数据对象 Bundle extras = getIntent().getExtras(); if (extras != null) { String receivedMessage = extras.getString("key_message"); } ``` 此部分描述了如何通过 `putExtra()` 方法来附带额外的信息给下一个界面处理。 --- #### 外部链接跳转案例 对于某些特殊场景下可能涉及跨应用或者基于 URI 方案定义触发行为的需求,可采用隐式意图配合特定协议地址完成操作。下面给出了一种典型例子说明怎样构建点击事件处理器从而导航至预设位置的同时还能捎带上必要的上下文资料。 ```kotlin binding.to.setOnClickListener { val intent = Intent().apply { this.action = Intent.ACTION_SEND this.data = Uri.parse("http://example.com") // 替换为目标 URL 地址 } context.startActivity(intent) } ``` 这里体现了另一种形式即设定 action 类型为 ACTION_SEND 及关联 web 链接作为 destination 参数实例化过程中的运用情景[^2]。 --- ### 总结 综上所述,在 Android 应用程序内部切换不同页面主要依赖于构造合适的 Intents 结合 startActivities API 达成目的;与此同时倘若存在交需求的话记得善加运用 putExtras 功能模块辅助传输所需变量集合即可达成预期效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值