美文网首页Android知识点滴
Android:StartActivies(Intent[] i

Android:StartActivies(Intent[] i

作者: 饱醉豚我去年买了个表 | 来源:发表于2016-09-18 14:54 被阅读820次
    startActivities
    void startActivities ([Intent[] intents, Bundle options)
        Launch multiple new activities. This is generally the same as calling [startActivity(Intent)])
    for the first Intent in the array, that activity during its creation calling [startActivity(Intent)]
    for the second entry, etc. Note that unlike that approach, generally none of the activities except the last in the array will be created at this point, but rather will be created when the user first visits them (due to pressing back from the activity on top).
    This method throws [ActivityNotFoundException]
    if there was no Activity found for *any* given Intent. In this case the state of the activity stack is undefined (some Intents in the list may be on it, some not), so you probably want to avoid such situations.
    

    启动多个新的activity,通常和我们调用startActivity(Intent)是一样的,但最开始创建的不是第一个Activity而是最后一个Activity,当按返回键后,会发现返回顺序和Intent[] intents中的顺序正好是相反的。

    来,举个栗子:

    Intent[] intents = new Intent[3];
    intents[0] = new Intent(MainActivity.this, Activity1.class);
    intents[1] = new Intent(MainActivity.this, Activity2.class);
    intents[2] = new Intent(MainActivity.this, Activity3.class);
    startActivities(intents);
    

    运行之后首先跳转的是Activity3,按返回键后跳转到了Activity2,再按返回键跳转到了Activity1,正好和我们Intent[] intents数组中的顺序是相反的,最后看下Log日志:

    09-18 14:49:22.976 3116-3116 E/TTT: Activity3 onCreate
    09-18 14:49:22.976 3116-3116 E/TTT: Activity3 onStart
    09-18 14:49:22.976 3116-3116 E/TTT: Activity3 onResume
    09-18 14:49:25.456 3116-3116 E/TTT: Activity2 onCreate
    09-18 14:49:25.456 3116-3116 E/TTT: Activity2 onStart
    09-18 14:49:25.461 3116-3116 E/TTT: Activity2 onResume
    09-18 14:49:26.771 3116-3116 E/TTT: Activity1 onCreate
    09-18 14:49:26.771 3116-3116 E/TTT: Activity1 onStart
    09-18 14:49:26.771 3116-3116 E/TTT: Activity1 onResume
    

    我们项目中是在推送消息的时候会用到StartActivies(Intent[] intents),

    Intent[] intents = new Intent[2];
    intents[0] = new Intent(context, GuideAct.class);
    intents[0].addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
    intents[1] = new Intent(context, Target.class);
    //应用在前台,直接startActivity(intents[1]),否则跳转的是startActivities(intents)
    if (Constant.isAppOpen) {    
    context.startActivity(intents[1]);
    } else {    
    context.startActivities(intents);
    }
    

    这样就可以实现当用户从推送消息中点进目标Activity并按返回键后,重新启动应用!

    相关文章

      网友评论

        本文标题:Android:StartActivies(Intent[] i

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