When the Activity's launchMode is set to singleTask, you should pay attention!

When developing an e-commerce APP application, in order to prevent the activity from being created multiple times, I set  Android :launchMode="singleTask" and the code is as follows:

[java]  view plain   copy
  print ?
  1. <!-- Main page -->   
  2.        <activity  
  3.            android:name="com.sondon.mayi.activity.MainTabActivity_"  
  4.            android:label="@string/app_name"  
  5.            android:launchMode="singleTask"  
  6.           >  

It was OK all the way through development, so I became careless.

Then when debugging umeng push, there was a problem that getIntent() could not obtain data. Various debugs and various Log output logs showed that there was data in the part where umeng processed the message, but there was no Intent in MainTabActivity. data.

umeng message processing part of the code:

[java]  view plain   copy
  print ?
  1.              /** 
  2.          * This Handler is called in BroadcastReceiver, so 
  3.          * If you need to start the Activity, you need to add Intent.FLAG_ACTIVITY_NEW_TASK 
  4.          * */  
  5.         UmengNotificationClickHandler notificationClickHandler = new UmengNotificationClickHandler(){  
  6.               
  7.             @Override  
  8.             public void launchApp(final Context context, final UMessage msg) {  
  9.                 new Handler(getMainLooper()).post(new Runnable() {  
  10.                     @Override  
  11.                     public void run() {  
  12.                           
  13.                         myprefs.position().put(1);  
  14.                         Intent intent = new Intent(context,MainTabActivity_.class);  
  15.                         intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
  16.                         for (Entry<String, String> entry : msg.extra.entrySet()) {  
  17.                             String key = entry.getKey();  
  18.                             String value = entry.getValue();  
  19.                              if ("code".equals(key.trim())) {  
  20.                              LogUtil.e("Umeng...launchApp","key  :"+key+"    value :"+value);  
  21.                              Bundle bundle=new Bundle();  
  22.                              bundle.putString("news_code",value);  
  23.                              intent.putExtras(bundle);  
  24.                              }  
  25.                         }  
  26.                         context.startActivity(intent);  
  27.                     }  
  28.                 });  
  29.             }  
  30.               
  31. //          @Override  
  32. //          public void openUrl(Context context, UMessage msg){   
  33. //              ToastUtils.show(context, msg.custom);  
  34. //              LogUtil.e("Umeng...openUrl :", msg.custom);  
  35. //          };  
  36. //            
  37. //          @Override  
  38. //          public void openActivity(Context context, UMessage msg){   
  39. //              ToastUtils.show(context, msg.custom);  
  40. //              LogUtil.e("Umeng... openActivity :", msg.custom);  
  41. //          };  
  42. //            
  43. //          @Override  
  44. //          public void dealWithCustomAction(final Context context, final UMessage msg) {   
  45. //              ToastUtils.show(context, msg.custom);  
  46. //              LogUtil.e("Umeng... dealWithCustomAction:", msg.custom);  
  47. //          }  
  48.         };  
  49.           
  50.     mPushAgent.setNotificationClickHandler(notificationClickHandler);  


折腾了很久,一直以为是umeng的问题,查了很多资料,搞了很久,最后才想起我这个activity的launchMode是singleTask,赶紧google了一下关于singleTask的资料,发现如下:


launchMode为singleTask的时候,通过Intent启到一个Activity,如果系统已经存在一个实例,系统就会将请求发送到这个实例上,但这个时候,系统就不会再调用通常情况下我们处理请求数据的onCreate方法,而是调用onNewIntent方法


于是赶紧在我的MainTabActivity页面中添加OnNewIntent方法,方法如下:

[java]  view plain   copy
  print ?
  1. //Activity的启动模式(launchMode),通过这个方法接受Intent  
  2.     @Override  
  3.     protected void onNewIntent(Intent intent) {  
  4.         super.onNewIntent(intent);  
  5.         LogUtil.e("MainTabActivity"" onNewIntent...");  
  6.         handleIntent(intent);  
  7.     }  

google的时候又发现了这样的提醒:

不要忘记,系统可能会随时杀掉后台运行的Activity,如果这一切发生,那么系统就会调用onCreate方法,而不调用onNewIntent方法,一个好的解决方法就是在onCreate和onNewIntent方法中调用同一个处理数据的方法,如下所示:


觉得别人考虑的真是缜密啊,佩服,赶紧给自己代码打上补丁吧,修改如下:

[java]  view plain   copy
  print ?
  1.     //Activity的启动模式(launchMode),通过这个方法接受Intent  
  2.     @Override  
  3.     protected void onNewIntent(Intent intent) {  
  4.         super.onNewIntent(intent);  
  5.         LogUtil.e("MainTabActivity"" onNewIntent...");  
  6.         handleIntent(intent);  
  7.     }  
  8.       
  9.     @Override  
  10.     protected void onStart() {  
  11.         super.onStart();  
  12.         LogUtil.e("MainTabActivity"" onStart...");  
  13.         handleIntent(getIntent());  
  14.     }  
  15.   
  16.     /** 
  17.      * @Author Cai Wenfeng 
  18.      * @Data_Time July 16, 2015 10:37:12 pm 
  19.      * @Description { Handle Intent } 
  20.      * @param intent 
  21.      */  
  22.     private void handleIntent(Intent intent){  
  23.         if(intent!=null){  
  24.             String news_code=intent.getExtras().getString("news_code");  
  25. //          LogUtil.e("MainTabActivity", "news_code  :"+news_code);  
  26.             myprefs.news_code().put(news_code);  
  27.         }  
  28.     }  

That's it. I learned a lot afterwards. Basic things are still very important. There are some things that are rarely used, so you should pay special attention to them!


Guess you like

Origin blog.csdn.net/H_shaohui/article/details/77379210