美文网首页安卓基础知识Android开发Android技术知识
一个SingleTask与跳转传值引发的血案

一个SingleTask与跳转传值引发的血案

作者: 代码咖啡 | 来源:发表于2017-03-08 16:47 被阅读657次

    此文原来是自己CSDN上的第一篇博客,因个人需要,将其复制到简书平台,另一方面也希望此文可以给安卓初学者在Activity启动模式中SingleTask模式下的传值提供思路参考,文风拙劣,欢迎指导。
    原文地址:
    http://blog.csdn.net/wj_november/article/details/49851117

    最近在做项目中遇到这样一个情况:

    • Activity A跳转到Activity B,Activity A设置为launchMode:singleTask
    • Activity B有一个ListView,点击ListView的一项,返回到Activity A中,同时传值点击的是那一项(见图:1-1,代码:onItemClick)
    • 然后在Activity B中Log出返回的值,但是无论如何就是获取不到(见代码:getBundle,见图:1-2)
    图1-1

    onItemClick

    @Override  
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {  
        Intent intent = new Intent(BActivity.this, AActivity.class);  
        Bundle bundle = new Bundle();  
        bundle.putString(StaticValues.KEY_CAR_SERIES, seriesList.get(position));  
        bundle.putInt(StaticValues.KEY_CAR_SERIES_ID, position);  
        intent.putExtras(bundle);  
        startActivity(intent);  
        finish();  
    }  
    

    getBundle

    private void getBundle() {  
        Bundle bundle = MainActivity.this.getIntent().getExtras();  
        if (null != bundle) {  
            Log.d("car", "bundle not null");  
            int carSeriesId = bundle.getInt(StaticValues.KEY_CAR_SERIES_ID);  
            String carSeries = bundle.getString(StaticValues.KEY_CAR_SERIES);  
            Log.d("car", carSeries + "--id=" + carSeriesId);  
        } else {  
            Log.d("car", "bundle null");  
        }  
    }  
    
    图1-2

    后来想到,Activity A使用了SingleTask的launchMode,猜想可能跟这个有关,在执行界面跳转的时候,不会生成新的Activity A实例,所以可能不会接收到传过来的Bundle里面的值。于是将Activity A的launchMode改为了Standard,果然,B传过来的值,A可以接收到(见图:1-3),验证了我的猜想。

    图1-3

    但是为什么使用了SingleTask就不能接收到Bundle的传值呢?
    带着疑问,我们打开了Google API的官网,找到了问题所在,罪魁祸首就是getIntent().我们现在看看我们使用的SingleTask:

    图1-4

    The system creates the activity at the root of a new task and routes the intent to it. However, if an instance of the activity already exists, the system routes the intent to existing instance through a call to its onNewIntent() method, rather than creating a new one.

    大致意思就是说一个Activity B如果跳转到另一个launchMode为SingleTask的Activity A时,如果task里面已经有Activity A,那么跳转时只需要调用它里面的onNewIntent()方法,而不是另外创建一个新的Activity A.

    那么onNewIntent()到底是什么?如何使用它呢?
    带着好奇,我们来查询onNewIntent()这个方法:

    图1-5

    onNewIntent() will be called on the existing instance with the Intent that was used to re-launch it.

    这句话的大致意思是说,如果你的Task里面已经有Activity A实例,且其他其他界面跳转到A时不会产生新的A的实例时,onNewIntent()会被调用,通常这时候这里面的Intent是用来重新启动那个已经存在的A实例的。

    An activity will always be paused before receiving a new intent, so you can count on onResume() being called after this method.

    我们的Activity A在接收到一个新的Intent的时候,会被pause掉,所以我们在调用onNewIntent()的时候,记得在之后调用onResume().(虽然读清楚了字面意思,但是还是不知道具体怎么操作-_-#)

    这里我们注意一下这句话:

    Note that getIntent() still returns the original Intent. You can use setIntent(Intent) to update it to this new Intent.

    我们之前在getBundle()方法里(见上面代码:getBundle)调用的是getIntent()方法,这个方法返回的是最原始启动Actvity A的Intent,而不是由Activity B跳转到Activity A使用的Intent。所以我们想要拿到Activity B跳到Activity A使用的Intent,我们要使用setIntent(Intent)方法。

    那么我们如何使用setIntent(Intent)呢?
    由上述表达的意思,我们隐约可以理解,setIntent(Intent)在onNewIntent(Intent)里面,那么setIntent方法的参数无疑使用的也是onNewIntent的参数。可是onNewIntent(Intent)方法是如何调用的呢?我们回到了上一个问题。于是带着思考,我们去度娘了一下,结果找到了这样的代码:

    @Override  
    protected void onNewIntent(Intent intent) {  
        super.onNewIntent(intent);  
        setIntent(intent);  
    } 
    

    表达的意思和上述onNewIntent的英文解释一样,当有一个Activity B跳转到我们的singleTask Activity A时,我们在getIntent()前会先执行onNewIntent()方法,在这个方法里,我们将接收到的新的Intent覆盖了第一次启动Activity A用的Intent,这样我们在getIntent()的时候,拿到的Intent就是带有B传给A带有Bundle值的Intent了。
    我们将onNewIntent(Intent)方法写在了我们的Activity A里面,成功的得到了B传给A的数据(效果如图:1-3)。

    相关文章

      网友评论

      • 007b89c4d4ca:activity四中启动模式,生命周期熟记,intent的理解加深,多谢
        代码咖啡:@90斤少女 不客气哈:smile:

      本文标题:一个SingleTask与跳转传值引发的血案

      本文链接:https://www.haomeiwen.com/subject/zegbgttx.html