Coa*_*alB 1 android android-intent
我在将单一任务活动重新放入视图时遇到了一些麻烦.
表现:
<activity
android:name=".test.MyActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:icon="@drawable/my_launcher"
android:label="@string/title_activity_my"
android:launchMode="singleTask"
android:taskAffinity=".myTask"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Black.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Run Code Online (Sandbox Code Playgroud)
MyActivity代码:
Intent i = new Intent(getApplicationContext(), MyActivity.class);
i.putExtra("blah", stuff);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK); // I have tried almost every flag combo here!
startActivity(i);
Run Code Online (Sandbox Code Playgroud)
场景:
在onBackPressed()上,活动调用moveTaskToBack(隐藏我的活动).收到特定的传入事件后,会调用上面的意图代码,但是没有发生!?!没有OnNewIntent()被触发,没有onCreate被激活......
但是:如果我还将一个单独的PendingIntent添加到Notification对象中,它可以工作!?
Intent notificationIntent = new Intent(getApplicationContext(),MyActivity.class);
notificationIntent.putExtra("blah", stuff);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification notif = new Notification(icon,text,when);
notif.flags |= Notification.FLAG_ONGOING_EVENT;
notif.setLatestEventInfo(getApplicationContext(), contentTitle, contentText, contentIntent);
m_notifMgr.notify(1, notif);
Run Code Online (Sandbox Code Playgroud)
我不明白为什么它可以从PendingIntent(触发onNewIntent)工作,但不能直接从startActivity调用.
请指教.谢谢.
更新(1):
我简化了问题并将相同的逻辑放入一个小测试应用程序中:
活动'A'= singleTask,有两个按钮:btnStartTimer用于启动计时器,btnMoveToBack用于将'A'向后移动.当计时器到期时,调用创建"B"的意图.
活动'B'=简单的'hello world'屏幕.
结果:
(i)如果我通过btnStartTimer单击启动计时器,并且不按btnMoveToBack,则调用目标代码并出现"B".
(ii)如果我通过btnStartTimer单击启动计时器,并按DO键btnMoveToBack,则仍会调用目标代码(由log msg表示)并且不显示"B" - 日志记录显示未调用的活动'B'onCreate.
这提出了一个问题 - 我是否真的可以从未显示的活动中处理意图?
我终于破解了这个.如图所示,无法从暂停的(singleTask)活动中触发意图(具有任何后果).因此,我唯一的选择是实现IntentService(利用Intent标志FLAG_ACTIVITY_CLEAR_TOP和FLAG_ACTIVITY_NEW_TASK),这反过来创建了"重新显示"我的singleTask活动所需的意图.
看起来像是一个用来敲碎核桃的大锤,但它是我能找到的唯一有效的方法.
| 归档时间: |
|
| 查看次数: |
1676 次 |
| 最近记录: |