在開發一個電商APP應用的時候,爲了不讓activity多次創建,所以設置了 android:launchMode="singleTask" 代碼如下:
<!-- 主頁面-->
<activity
android:name="com.sondon.mayi.activity.MainTabActivity_"
android:label="@string/app_name"
android:launchMode="singleTask"
>
一路開發下來都是OK,也因此麻痹大意了。
然後在調試umeng推送的時候,出現了getIntent()獲取不到數據的問題,各種debug,各種Log輸出日誌,顯示都是在umeng處理消息的部分是有數據的,但在MainTabActivity就是沒有Intent數據。
umeng處理消息部分代碼:
/**
* 該Handler是在BroadcastReceiver中被調用,故
* 如果需啓動Activity,需添加Intent.FLAG_ACTIVITY_NEW_TASK
* */
UmengNotificationClickHandler notificationClickHandler = new UmengNotificationClickHandler(){
@Override
public void launchApp(final Context context, final UMessage msg) {
new Handler(getMainLooper()).post(new Runnable() {
@Override
public void run() {
myprefs.position().put(1);
Intent intent = new Intent(context,MainTabActivity_.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
for (Entry<String, String> entry : msg.extra.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
if ("code".equals(key.trim())) {
LogUtil.e("Umeng...launchApp","key :"+key+" value :"+value);
Bundle bundle=new Bundle();
bundle.putString("news_code",value);
intent.putExtras(bundle);
}
}
context.startActivity(intent);
}
});
}
// @Override
// public void openUrl(Context context, UMessage msg){
// ToastUtils.show(context, msg.custom);
// LogUtil.e("Umeng...openUrl :", msg.custom);
// };
//
// @Override
// public void openActivity(Context context, UMessage msg){
// ToastUtils.show(context, msg.custom);
// LogUtil.e("Umeng... openActivity :", msg.custom);
// };
//
// @Override
// public void dealWithCustomAction(final Context context, final UMessage msg) {
// ToastUtils.show(context, msg.custom);
// LogUtil.e("Umeng... dealWithCustomAction:", msg.custom);
// }
};
mPushAgent.setNotificationClickHandler(notificationClickHandler);
launchMode爲singleTask的時候,通過Intent啓到一個Activity,如果系統已經存在一個實例,系統就會將請求發送到這個實例上,但這個時候,系統就不會再調用通常情況下我們處理請求數據的onCreate方法,而是調用onNewIntent方法
於是趕緊在我的MainTabActivity頁面中添加OnNewIntent方法,方法如下:
//Activity的啓動模式(launchMode),通過這個方法接受Intent
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
LogUtil.e("MainTabActivity", " onNewIntent...");
handleIntent(intent);
}
google的時候又發現了這樣的提醒:
不要忘記,系統可能會隨時殺掉後臺運行的Activity,如果這一切發生,那麼系統就會調用onCreate方法,而不調用onNewIntent方法,一個好的解決方法就是在onCreate和onNewIntent方法中調用同一個處理數據的方法,如下所示:
覺得別人考慮的真是縝密啊,佩服,趕緊給自己代碼打上補丁吧,修改如下:
//Activity的啓動模式(launchMode),通過這個方法接受Intent
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
LogUtil.e("MainTabActivity", " onNewIntent...");
handleIntent(intent);
}
@Override
protected void onStart() {
super.onStart();
LogUtil.e("MainTabActivity", " onStart...");
handleIntent(getIntent());
}
/**
* @Author 蔡文鋒
* @Data_Time 2015年7月16日 下午10:37:12
* @Description { 處理Intent }
* @param intent
*/
private void handleIntent(Intent intent){
if(intent!=null){
String news_code=intent.getExtras().getString("news_code");
// LogUtil.e("MainTabActivity", "news_code :"+news_code);
myprefs.news_code().put(news_code);
}
}
如此就搞定了,事後感悟良多啊,基礎的東西還是很重要的,有一些比較少用的東西,要特別的留一個心眼!