2012-11-09 64 views
1

我需要一些解釋/建議,也許一些示例如何使用Fragments替換我的ActivityGroup類。我已經使用已棄用的TabActivityActivityGroup合併,因此我在一年前編寫了該應用程序,因此在進入不同的活動時仍然可以保留這些標籤。所以現在我想重寫我的應用程序並使用新的API,並且我很好奇如何使用片段活動重寫方法和整個想法,並在與應用程序的整個交互過程中保留選項卡欄。我不需要用新標籤替換TabActivity,也不需要ActionBar。這裏是一個即時通訊使用,以保持標籤欄在應用程序的底部我TabGroupActivity的例子:Android使用Fragments/FragmentsManager替換ActivityGroup

public class TabGroupActivity extends ActivityGroup { 

    private ArrayList<String> mIdList; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState);  
     if (mIdList == null) mIdList = new ArrayList<String>(); 
    } 

    /* 
    * This is called when a child activity of this one calls its finish method. 
    * This implementation calls {@link LocalActivityManager#destroyActivity} on the child activity 
    * and starts the previous activity. 
    * If the last child activity just called finish(),this activity (the parent), 
    * calls finish to finish the entire group. 
    */ 
    @Override 
    public void finishFromChild(Activity child) { 
     LocalActivityManager manager = getLocalActivityManager(); 
     int index = mIdList.size()-1; 

     if(index < 1) { 
       finish(); 
       return; 
     } 

     try { 
      manager.destroyActivity(mIdList.get(index), true); 
      mIdList.remove(index); 
      index--; 
      String lastId = mIdList.get(index); 
      if(manager.getActivity(lastId).getIntent() == null){ 
       finish(); 
      } else if(manager.getActivity(lastId).getIntent() != null){ 
       Intent lastIntent = manager.getActivity(lastId).getIntent(); 
       Window newWindow = manager.startActivity(lastId, lastIntent); 
       setContentView(newWindow.getDecorView()); 
      } 
     }catch (NullPointerException e){ 
      finish(); 
     } 

    } 

    /* 
    * Starts an Activity as a child Activity to this. 
    * @param Id Unique identifier of the activity to be started. 
    * @param intent The Intent describing the activity to be started. 
    * @throws android.content.ActivityNotFoundException. 
    */ 
    public void startChildActivity(String Id, Intent intent) {  
     Window window = getLocalActivityManager().startActivity(Id,intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)); 
     if (window != null) { 
      mIdList.add(Id); 
      setContentView(window.getDecorView()); 
     }  
    } 

    /* 
    * The primary purpose is to prevent systems before android.os.Build.VERSION_CODES.ECLAIR 
    * from calling their default KeyEvent.KEYCODE_BACK during onKeyDown. 
    */ 
    @Override 
    public boolean onKeyDown(int keyCode, KeyEvent event) { 
     if (keyCode == KeyEvent.KEYCODE_BACK) { 
      //preventing default implementation previous to android.os.Build.VERSION_CODES.ECLAIR 
      return true; 
     } 
     return super.onKeyDown(keyCode, event); 
    } 

    /* 
    * Overrides the default implementation for KeyEvent.KEYCODE_BACK 
    * so that all systems call onBackPressed(). 
    */ 
    @Override 
    public boolean onKeyUp(int keyCode, KeyEvent event) { 
     if (keyCode == KeyEvent.KEYCODE_BACK) { 
      onBackPressed(); 
      return true; 
     } 
     return super.onKeyUp(keyCode, event); 
    } 

    /* 
    * If a Child Activity handles KeyEvent.KEYCODE_BACK. 
    * Simply override and add this method. 
    */ 
    @Override 
    public void onBackPressed() { 
     int length = mIdList.size(); 
     if (length >=1) { 
      Activity current = getLocalActivityManager().getActivity(mIdList.get(length-1)); 
      try{ 
       current.finish(); 
      }catch(Exception e){ 
       finish(); 
      } 

     } 
    } 
} 

所以基本上我需要一些幫助瞭解如何達到相同的交互,在Instagram的應用程序。

各種幫助真的很感謝!

回答

0

在我的情況下,我使用Android支持庫中的FragmentManager V4和帶碎片的tabhost,並且完成了這些工作。如果有人需要一個例子,您可以在Android開發人員博客中找到它,或者只是在Google上搜索Android Fragments示例,您會發現一些有用的東西。 :)