0

我正在開發包含TabHost一個應用程序,在這些標籤我有一個ActivityGroup之一,從這個ActivityGroup,我發動另一SubActivity(讓我們說,我啓動了Activity A),以及直到這一切都好。導航在的ActivityGroup活動之間

問題是,當我按下後退按鈕,則CurrentActivity(Activity A)被破壞,但ParentActivity(該ActivityGroup)不恢復,以及應用程序展示只是一個空窗與我的應用程序的標題( 「我的應用標題」)。

從我ActivityGroup啓動Activity A中的代碼是:

View view = getLocalActivityManager().startActivity(id,newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)) .getDecorView(); 
this.setContentView(view); 

和我在ActivityGroupoverrided方法onKeyDown這樣的:

@Override 
    public boolean onKeyDown(int keyCode, KeyEvent event) { 
     Log.i(TAG, "onKeyDown"); 
     if(keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0){ 
      Activity current = getLocalActivityManager().getCurrentActivity(); 
      Log.i(TAG, current.getIntent().getStringExtra("id")); 
      current.finish(); 
      return true; 
     } 
     return super.onKeyDown(keyCode, event); 
    } 

但似乎方法onKeyDown永遠不會被調用,因爲ai沒有顯示日誌「onKeyDown」。

和logcat的顯示器只是這樣的:

01-05 11:04:38.012: W/KeyCharacterMap(401): No keyboard for id 0 
01-05 11:04:38.012: W/KeyCharacterMap(401): Using default keymap: /system/usr/keychars/qwerty.kcm.bin 

我想是顯示ActivityGroup當我Activity A被破壞。

NB我的應用水平是4: * 的Android 1.6 *,所以我不能override方法onBackPressed()

謝謝大家的幫助

-----------------------------------編輯------------- ---------------------------

我已經加入我的onKeyDown這樣的代碼我Activity答:

@覆蓋 公共布爾的onkeydown(INT的keyCode,KeyEvent的事件){

if(keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0){ 
     ParentActivity parentActivity = (ParentActivity) this.getParent(); 
     parentActivity.onKeyDown(keyCode, event); 
     return true; 
    } 
    return super.onKeyDown(keyCode, event); 
} 

而且在我ParentActivity,我有:

@Override 
    public boolean onKeyDown(int keyCode, KeyEvent event) { 
     if(keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0){ 
      Log.i(TAG, "onKeyDown"); 
      int len = idOfSubActivities.size(); 
      String idOfCurrentActivity = idOfSubActivities.get(len-1); 
      Activity currentActivity = getLocalActivityManager().getActivity(idOfCurrentActivity); 
      currentActivity.finish(); 
      idOfSubActivities.remove(len - 1); 
      return true; 
     } 
     return super.onKeyDown(keyCode, event); 
    } 

我得到了相同的結果,Activity A被停止,但它仍然給我個空窗我的應用程序的標題,它不顯示我ActivityGroupParentActivity

回答

1

當我第一次開始試驗ActivityGroup s時遇到類似問題。問題是您需要將您的onKeyDown()放入您的Activity。但是,您需要Activity才能參考ActivityGroup。然後,當您按回時,只需在ActivityGroup中撥打您自己的onBack()即可。

(EDIT)下面是一個示例爲您

下面是一個剝離下來的ActivityGroup代碼,處理導航和歷史在我的應用程序。它已經進行了調整,所以可能有錯誤。注意一些更好的點。

public class MyGroup extends ActivityGroup 
{ 
/** Static Reference to this Group. */ 
    static MyGroup instance; 
/** Keeps Track of the History as a Stack. */ 
    private ArrayList<View> myActivityHistory; 

    @Override protected void onCreate(final Bundle savedInstanceState) 
    {//Call the Base Implementation 
     super.onCreate(savedInstanceState); 

    // Initialize the Activity History 
     myActivityHistory = new ArrayList<View>(); 

    // Build the Intent 
     Intent _root = null; 
    //Lists the Applications 
     _root = new Intent(this, MyActivity.class); 
    // Send the Index to the Child Activity 
     _root.setAction(Intent.ACTION_VIEW); 
    // Forward the Extras, if they are there 
    // Start the root Activity within the Group and get its View 
     final View _view = getLocalActivityManager().startActivity("App Preferences", _root).getDecorView(); 
    // Start the History 
     addNewLevel(_view); 
    } 

    /** 
    * Gets the instance of the {@link ApplicationGroup} that the child Activity 
    * belongs to. 
    * 
    * @param index 
    * The Group that was passed to the child in the {@link android.content.Intent 
    * Intent} via an Extra (int). 
    * @return 
    * <b>ApplicationGroup -</b> The group that this child was assigned to. 
    */ 
    static public ApplicationGroup getGroup() 
    { if (instance != null) 
      return instance; 
    } 

    /** 
    * Allows the Child to replace the {@link ApplicationGroup}'s current 
    * {@link android.view.View View} with the specified View. This is 
    * intended to be used specifically by the Child. 
    * 
    * @param withView 
    * The View to use for replacement. 
    */ 
    public void addNewLevel(final View withView) 
    {//Adds the old one to history 
     myActivityHistory.add(withView); 
    // Changes this Groups View to the new View. 
     setContentView(withView); 
    } 

    /** 
    * Takes the specified {@link android.app.ActivityGroup ActivityGroup} back 
    * one step in the History to the previous {@link android.view.View View}. 
    */ 
    public void back() 
    { Log.d("Group", "Back overridden"); 
     //If there are more than one screen 
     if (myActivityHistory.size() > 1) 
     { Log.d("Group", "History called"); 
     // Remove the most recent View 
      myActivityHistory.remove(myActivityHistory.size()-1); 
     // Change the View back. 
      setContentView(myActivityHistory.get(myActivityHistory.size()-1)); 
     } 
    // Otherwise Exit 
     else 
     { Log.d("Group", "Program finished"); 
      finish(); 
     } 
    } 

} 

接下來是該活動的相關代碼:

public boolean onKeyDown(int keyCode, KeyEvent event) 
{//If back was pressed 
    if (keyCode==KeyEvent.KEYCODE_BACK) 
    { MyGroup.getGroup().back(); 
     return true; 
    } 
    return super.onKeyDown(keyCode, event); 
} 

只要確保你是不是KeyDownListener設置爲任何有趣的,它應該工作的罰款。 :)我所做的更改是因爲我實際上將它們放在一個組的數組中(一次3個)。從本質上講,只要使組成一個Singleton,以便始終可以擁有相同的實例,並保存一個視圖數組,以便擁有歷史記錄。然後在單擊後退或添加視圖時引用歷史記錄。

希望這有助於 FuzzicalLogic

+0

我會嘗試這個,感謝您的回覆:) – Houcine 2012-01-05 12:39:45

+0

請看到我的編輯,我做了什麼,你告訴我,我得到了相同的結果,能否請您提供一些你提到的代碼有哪些?謝謝 – Houcine 2012-01-05 12:51:43

+0

是的,給我幾個小時來抓住它。 (我在工作,我使用它的應用程序是在家裏)... – 2012-01-05 13:20:28