Intent、Bundle

1、Intent封裝

  1. public class IntentHelper { 
  2.     private Intent intent; 
  3.     private static IntentHelper intentHelper; 
  4.     private IntentHelper(){ 
  5.         intent = new Intent(); 
  6.     } 
  7.      
  8.     public static IntentHelper getInstance(){ 
  9.         if(intentHelper == null){ 
  10.             intentHelper = new IntentHelper(); 
  11.         } 
  12.         return intentHelper; 
  13.     } 
  14.      
  15.     public Intent getIntent(Context packageContext, Class<?> cls){ 
  16.         intent.setClass(packageContext, cls); 
  17.         return intent; 
  18.     } 
  19.      
  20.     public Intent getIntentClearPreActivity(Context packageContext, Class<?> cls){ 
  21.         Intent intent = new Intent();  
  22.         intent.setClass(packageContext, cls); 
  23.         intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);  
  24.         return intent; 
  25.     } 
  26.      
  27.     public Intent getNewIntent(Context packageContext, Class<?> cls){ 
  28.         Intent intentNew = new Intent(); 
  29.         intentNew.setClass(packageContext, cls); 
  30.         return intentNew; 
  31.     } 

2、Activity的兩種啓動模式:

  1. FLAG.ACTIVITY_CLEAR_TOP         
  2. FLAG_ACTIVITY_REORDER_TO_FRONT 

如果已經啓動了四個Activity:A,B,C,D 在D Activity裏,我們要跳轉到B Activity,同時希望C finish掉,可以在startActivity(intent)裏的intent裏添加flags標記

  1. Intent intent = new Intent(this, B.class);    
  2. intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);   
  3. startActivity(intent);  

 

 這樣啓動B Activity,就會把DCfinished掉,如果你的B Activity的啓動模式是默認的(multiple),B Activityfinished掉,再啓動一個新的Activity B。如果不想重新再創建一個新的B Activity,則在上面的代碼裏再加上

  1. intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);  

 

3、bundle傳值

  1. Intent intent = new Intent(); 
  2. Bundle bundle = new Bundle(); 
  3. bundle.putString("Tag""mapView");   
  4. intent.setClass(A.this, B.class); 
  5. intent.putExtras(bundle); 
  6. startActivity(intent); 
  1. bundle = this.getIntent().getExtras(); 
  2. bundle.getString("Tag");

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章