我正在开发一个包含TabHost
的应用程序,在其中一个选项卡中我有一个ActivityGroup
,从这个ActivityGroup
,我启动了另一个SubActivity
(假设我启动了一个Activity
A(,在此之前,一切都很好。
问题是当我按下后退按钮时,当前活动(Activity
A(被销毁,但父活动(ActivityGroup
(没有恢复,并且应用程序仅显示一个空窗口,其中包含我的应用程序的标题(">我的应用程序标题"(。
从我的ActivityGroup
启动Activity
A 的代码是:
View view = getLocalActivityManager().startActivity(id,newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)) .getDecorView();
this.setContentView(view);
我已经在我的ActivityGroup
中overrided
了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
,因为 i 没有显示日志"onKeyDown"。
日志猫只显示这个:
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
我想要的是在我的Activity
A 被销毁时显示ActivityGroup
。
注意:我的应用程序级别是4:*Android 1.6*,所以我无法override
方法onBackPressed()
谢谢大家的帮助
-----------------------------------编辑----------------------------------------
我已经在我的Activity
A 上添加了我的onKeyDown
的代码:
@Override public boolean onKeyDown(int keyCode, KeyEvent event( {
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 停止了,但它仍然给了我带有我的应用程序标题的空窗口,并且它不显示我的ActivityGroup
(ParentActivity
(
当我第一次开始尝试ActivityGroup
时,我遇到了类似的问题。问题是您需要将onKeyDown()
放在Activity
中。但是,您需要Activity
具有对ActivityGroup
的引用。然后,当您按回去时,只需在ActivityGroup
中调用您自己的onBack()
即可。
(编辑(这是给你的一个示例
下面是精简的 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 个(。本质上,只需将组设置为单例,以便您始终可以拥有相同的实例,并保留一个视图数组,以便您拥有历史记录。然后在单击"上一步"或添加视图时引用"历史记录"。
希望这有帮助,模糊逻辑