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