Activity之间的跳转

本文介绍了Android中Intent类的使用方法,包括显示Intent和隐示Intent的区别及应用,并详细讲解了如何利用Intent进行Activity间的跳转及传值。

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

如果要实现Activity之间的跳转,需要一个Intent类,来帮助我们,Intent类是Android程序中各组件的进行交互的一种重要方式,它不仅可以进行跳转还可以在跳转的过程中传值。

Intent类

Intent类分为2种:显示Intent隐示Intent

显示Intent

Intent intent=new Intent(this.getApplicationContext(),AmapActivity.class)
startActivity(intent)

第一个参数是本类作为上下文,第二个参数为要跳转的Activity的字节码

隐示Intent

为什么叫隐示,因为它跳转的参数不会很明确的指定。

使用隐示跳转首先打开AndroidManifest.xml,在activity节点中找category子节点,里面的Name属性就是跳转的参数。把它复制下来,如果没有该节点可以自己创建一个。

<category android:name="android.intent.category.MyNode"/>

最后的MyNode可以自己定义

跳转

Intent intent = new Intent("android.intent.category.MyNode");
startActivity(intent);

隐示跳转比显示跳转的参数少,目的也不是很明确。

一个Intent里只能指定action,但是可以指定多个category

Intent intent = new Intent("android.intent.category.MyNode");
intent.addCategory("android.intent.category.MyNode");
startActivity(intent);

用Intent跳转参数

使用Intent的putExtra方法:

Intent intent = new Intent("android.intent.category.MyNode");
intent.putExtra("key","value");
startActivity(intent);

获取方法:

Intent intent=getIntent();
String data=intent.getStringExtra("key");
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值