简体   繁体   English

Android活动意图启动

[英]Android activity intent launching

I'm stuck for a couple of hours on a stupid problem. 我在一个愚蠢的问题上呆了几个小时。

My application is composed by 2 activities: A (master) and B. There is also one service S. The "normal" tasks stack is AB and S is running in background. 我的应用程序由2个活动组成:A(主)和B。还有一个服务S。“正常”任务堆栈是AB,S在后台运行。

My issue is appeared when I would like directly reach the activity B with a notification. 我想通过通知直接到达活动B时出现我的问题。 I have to construct the tasks stack to obtain AB in order to keep the default behaviour of the return button. 我必须构造任务堆栈以获得AB才能保留返回按钮的默认行为。 (I want the user could back to A). (我希望用户可以回到A)。

Because I have use the API 7, I can't use an intent array as show in the android developpers tutorial http://developer.android.com/guide/topics/ui/notifiers/notifications.html . 因为我已经使用了API 7,所以无法使用intent数组,如android开发人员教程http://developer.android.com/guide/topics/ui/notifiers/notifications.html中所示 So I have decided to add a custom action (S.ACTION_CUSTOM) in the intent sent by the notification in order to distinguish this case in the onCreate() of the activity A. 因此,我决定在通知发送的意图中添加自定义操作(S.ACTION_CUSTOM),以便在活动A的onCreate()中区分这种情况。

Intent associated to the notification declared in the service S : 与服务S中声明的通知相关的意图:

Intent notificationIntent = new Intent(this, A.class);
       notificationIntent.setAction(ACTION_CUSTOM);

Now in A, at the end of the onCreate() method, I add: 现在在A中,在onCreate()方法的末尾,添加:

if (S.ACTION_CUSTOM.equals(getIntent().getAction())) {
        Intent intent = new Intent(this, B.class);
        intent.setAction(S.ACTION_CUSTOM);
        startActivity(intent); 
 }

This code works to go directly to B from notification and it authorize the return to A. But once I have use the notification "shorcut", then B is directly reached when the application start, even from the home menu. 这段代码可以直接从通知中转到B,并授权返回到A。但是一旦我使用了通知“ shorcut”,则在应用程序启动时就可以直接到达B,即使从主菜单也可以。 I have tried many things to understand what happen but even if I add getIntent().setAction(ANYTHING) at the end of the if statement in A, the behaviour is the same. 我已经尝试了很多事情来了解发生了什么,但是即使我在A的if语句的末尾添加了getIntent()。setAction(ANYTHING),其行为也是一样的。

Could anyone tell me what happening with my code please ? 谁能告诉我我的代码发生了什么? Morover I'm open minded with an other method to get my wishes. Morover我对其他方法持开放态度,以实现自己的愿望。

Thanks. 谢谢。

Edit: 编辑:

Part of the XML manifest: XML清单的一部分:

<activity
        android:name=".A"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
</activity>
<activity
        android:name=".B"
        android:label="@string/app_name" >
</activity>

But I think intent filters have nothing to do with my problem. 但是我认为意图过滤器与我的问题无关。

With more tests, I have found the cause and a solution. 通过更多的测试,我找到了原因和解决方案。

In fact, when I said I relaunched the application from the home menu, It was not true. 实际上,当我说我从主菜单重新启动该应用程序时,这不是事实。 I had relauched it from the "recent activities" (long press on home). 我从“最近的活动”(长按主页)中重新启动了它。 However, it seems that android, in this case, launches the application with the last intent which did ran it. 但是,在这种情况下,似乎android会以运行它的最后意图启动应用程序。 So it works fine from the home (because a new intent with Intent.ACTION_MAIN is fire) but not from recent activities because an intent with the same action as precedently is fired (ACTION_CUSTOM in my case) again and again. 因此,它可以在家中正常工作(因为使用Intent.ACTION_MAIN的新意图被触发),但不能用于最近的活动,因为具有与先前相同动作的意图被反复触发(在我的情况下为ACTION_CUSTOM)。

In order to differenciate intent from "recent activies" and "notification" I have used flags. 为了区分意图与“近期活动”和“通知”,我使用了标志。 In the case of recent activities, the intent has the flag Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY. 对于最近的活动,该意图具有标志Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY。

So I fixe my code by add a condition in the if statement: 因此,我通过在if语句中添加条件来修复代码:

if (S.ACTION_CUSTOM.equals(getIntent().getAction()) && (getIntent().getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) == 0) {
        startActivity(getIntent().setClass(this, B.class));  
}

My real code is a little more complicated with 4 activities instead of 2 but all works fine, each activity transmits the intent to the next. 我的实际代码稍微有点复杂,有4个活动,而不是2个活动,但是一切正常,每个活动都将意图传达给下一个活动。 The stack is well builded and when the user start the application from elsewhere than the notification, the default behaviour is kept. 堆栈构建良好,当用户从通知以外的其他位置启动应用程序时,将保留默认行为。

Hope this will help someone to avoid to lose his time to understand why intents seems crazy. 希望这可以帮助某人避免浪费时间去理解为什么意图似乎很疯狂。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM