Activity 跳转详解
大家好,又见面了,我是你们的朋友全栈君。
Activity 跳转详解 你好! 我是 Graydalf ,有可能也叫 Gdalf ~
今天被朋友问到如何设置一个广播来启动一个应用并显示数据,于是将自己了解到的记录下来,有什么较为 DEMO 的地方希望你能留言告诉我,谢谢。
本节说明Activity 跳转的方式跳转传值问题(包括非 Activity 的跳转到 Activity)跳转传递值时生命周期回调函数调用情况1. 显示跳转 通过字节码方式进行跳转,需要获取到字节码,所以多用于工程内跳转。
逻辑步骤:
通过Activity的实现类对象调用 startActivity(Intent intent) 方法跳转然后需要创建一个Intent对象 Intent i = new Intent(getApplicationContext(), MyActivity.class) ,参数1 可以使用通用的Context对象,参数2 则是需要跳转到的Activity字节码对象可以在Intent对象中存放数据 i.putExtra(key, value)然后再调转到的Activity中使用 getIntent().getStringExtra(key) 等方法来获取数据2. 隐式跳转 通过意图拦截器
逻辑步骤:
配置意图拦截器代码语言:javascript代码运行次数:0运行复制
通过Activity的实现类对象调用 startActivity(Intent intent) 方法跳转然后需要创建一个Intent对象 Intent i = new Intent() 设置请求字串 i.setAction("android.intent.action.MyActivity") 设置请求类型 i.addCategory("android.intent.category.DEFAULT")设置Data和Type i.setDataAndType(Uri.parse("src:"+"values"), "data/url") ,注意不能分别调用 setData(uri) 和 setType(str) 方法,方法内部互相置空,列出其中一个的源码解释:代码语言:javascript代码运行次数:0运行复制public Intent setType(String type) {
mData = null;//这里置空了对方
mType = type;
return this;
}然后再调转到的Activity中使用 getIntent().getStringExtra(key) 等方法来获取数据非Activity跳转到Activity我们用一个实例来讲解这种情况下遇到的问题
广播监视短信,启动Activity并且显示短信,流程图如下:
Created with Raphaël 2.1.0 Received sms Receiver onReceive Start Activity Show Message
1. Create BroadCastReceiver代码语言:javascript代码运行次数:0运行复制public class SmsReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Object[] objects = (Object[])intent.getExtras().get("pdus");
for(Object obj:objects) {
SmsMessage message = SmsMessage.createFromPdu((byte[])obj);
//获取发送人号码
message.getOriginatingAddress();
//获取内容
String messageBody = message.getMessageBody();
Intent i = new Intent(context, MainActivity.class);
i.putExtra("sms", messageBody);
//此处有BUG,我们在后面说明
context.startActivity(i);
Toast.makeText(context, "接收短信完毕:"+messageBody, Toast.LENGTH_SHORT).show();
//这里演示,要是短信长度超过一条我就只取第一条
break;
}
}
}2. Register BroadCastReceiver代码语言:javascript代码运行次数:0运行复制
3. 创建MyActivity故事情节可能会很波折,这是因为涉及到:
getIntent() 获取Intent异常非Activity对象调用 startActivity() Activity重复启动周期 在onCreate()方法中处理显示代码语言:javascript代码运行次数:0运行复制public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent i = getIntent();
if (i != null) {
TextView tv = (TextView) findViewById(R.id.tv);
tv.setText(i.getStringExtra("sms"));
}
}这时候我们让程序跑起来,系统向我们我们报错
代码语言:javascript代码运行次数:0运行复制erro: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?上面的意思是说我们在非Activity context中调用 startActivity(),必须申明 FLAG_ACTIVITY_NEW_TASK 标记
这是什么意思呢,我们 Ctrl + 左键 进入 context.startActivity(i) 内部查看下源码
代码语言:javascript代码运行次数:0运行复制/** * ... *
Note that if this method is being called from outside of an * {@link android.app.Activity} Context, then the Intent must include * the {@link Intent#FLAG_ACTIVITY_NEW_TASK} launch flag. This is because, * without being started from an existing Activity, there is no existing * task in which to place the new activity and thus it needs to be placed * in its own separate task. * ... */
public abstract void startActivity(Intent intent, Bundle options);
/* 若使用Activity之外的上下文对象启动一个Activity,则必须让Intent拥有 FLAG_ACTIVITY_NEW_TASK 启动标识,这是为了: 1.若已经有此Activity对象存在(也就是存在一个放置此Activity的任务栈),则在其任务栈中放置新的Activity对象 2.若没有现有的Activity对象存在(任务栈也不存在),因此需要将其放置在其自己独立的任务栈中。 */所以我们在 SmsReceiver 的 onReceive() 方法中调用 context.startActivity(i) 前加上 i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK) ,然后再次运行程序,模拟发送短信,此时我们能够接收到短信并且显示出来。
在onResume()方法中处理显示 但是上面的写法对用户的体验非常不好,因为每条短信都会重新创建一个Activity对象压入任务栈,我们要是想不创建新的Activity只在当前Activity中显示又该如何做呢?
首先想到的是在将Activity的启动模式设置成 android:launchMode="singleTop" 这代表任务栈栈顶只能存在一个此Activity对象,这样我们在重复跳转的时候就不会重新创建了,设置如下:
代码语言:javascript代码运行次数:0运行复制
这时我们运行程序并且模拟发送短信,发现没有显示短信内容,这是因为我们的显示短信代码在onCreate()方法中,此方法只能在Activity被创建时调用,这里因为 singleTop 启动模式并没有重新创建Activity,我们将7个生命周期回调方法都打上Log,发现当不创建新Activity对象的前提下调用 startActivity() 方法,声明周期函数只会先执行 onPause() 再执行 onResume ,所有我们将 onCreate() 中的显示代码移动到 onResume() 中来,再次测试,新的问题又出现啦,打上Log明明 onResume() 执行了,死活不出结果!!(波折么?)
getIntent()方法的特点我们将显示代码打上断点可以观测到,每次启动时, getIntent() 取得的方法总是 null (注:若你在模拟发送短信前,应用已经关闭,那么会回显示第一次的数据,再次发送短信取得的都是第一次的数据),我们继续 Ctrl + 左键 进入 getIntent() 方法内部查看源码:
代码语言:javascript代码运行次数:0运行复制/** Return the intent that started this activity. */
public Intent getIntent() {
return mIntent;
}
/* 翻译:返回启动Activity时的intent */光看这个你很难理解到什么叫返回启动时候的intent,本可以最简洁地口头描述给你看,但是这里还是准备用事实说话,我们继续查找名称中带有intent的方法,果然找到个文字叙述比较多且痛快的方法 onNewIntent() 的,如下:
代码语言:javascript代码运行次数:0运行复制/** * This is called for activities that set launchMode to "singleTop" in * their package, or if a client used the {@link Intent#FLAG_ACTIVITY_SINGLE_TOP} * flag when calling {@link #startActivity}. In either case, when the * activity is re-launched while at the top of the activity stack instead * of a new instance of the activity being started, onNewIntent() will be * called on the existing instance with the Intent that was used to * re-launch it. * *
An activity will always be paused before receiving a new intent, so * you can count on {@link #onResume} being called after this method. * *
Note that {@link #getIntent} still returns the original Intent. You * can use {@link #setIntent} to update it to this new Intent. */
protected void onNewIntent(Intent intent) {
}
/* 第一句话就很给力,描述了此方法可能的使用场景,正好符合,翻译:当我们调用一个 singleTop 启动模式的 Activity,或者调用 startActivity(intent) 方法时参数为标识 FLAG_ACTIVITY_SINGLE_TOP 的 Intent 对象时。 然后是说:当已经有此 Activity 实例存在栈顶时,上面两种情况都会导致 onNewIntent() 方法被调用。 下面两个段落是说: 1. Activity 将总是被 paused 之后才去接收一个新的intent,所以你可以等此方法(onNewIntent)被调用完毕时,在 onResume() 方法中去写自己的代码。 2. getIntent 方法总是返回原来的值,你可以使用 setIntent() 方法去更新一个新的intent。 */看完解释已经很明白了,我们现在修改重写 onNewIntent(intent) 方法如下:
代码语言:javascript代码运行次数:0运行复制@Override
protected void onNewIntent(Intent intent) {
setIntent(intent);
}现在再来运行程序,完美~
谢阅!
有什么好的建议和疑问可以在下面留言。
GitHub,才是男人的浪漫!
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/143179.html原文链接:https://javaforall.cn
天下手游恶来怎么样 恶来厉害吗|联想N50-80-IFI参数