Android ActivityGroup的onBackPressed()行为
快速提问:我有一个活动小组。在这个活动组中,我有一个活动。如果我在活动中按back键。调用活动的onBackPressed方法-而不是Activitygroups onBackPressed-这是为什么 编辑:得到了我的答案,但问题仍然存在。以下是我的原始版本的代码和说明: 我在TabHost中使用ActivityGroups,因此被“强制”重写onBackPressed。通过按手机上的后退键和tabhost上的选项卡,我可以毫无问题地浏览我的应用程序。但我无法在按下后与界面交互。 一旦我再次按下tabhost上的一个选项卡,我就可以像平常一样与任何东西进行交互。为什么会这样?我是否需要覆盖onResume 相关代码 设置活动组:Android ActivityGroup的onBackPressed()行为,android,activitygroup,Android,Activitygroup,快速提问:我有一个活动小组。在这个活动组中,我有一个活动。如果我在活动中按back键。调用活动的onBackPressed方法-而不是Activitygroups onBackPressed-这是为什么 编辑:得到了我的答案,但问题仍然存在。以下是我的原始版本的代码和说明: 我在TabHost中使用ActivityGroups,因此被“强制”重写onBackPressed。通过按手机上的后退键和tabhost上的选项卡,我可以毫无问题地浏览我的应用程序。但我无法在按下后与界面交互。 一旦我再次按
public class SettingsActivityGroup extends ActivityGroup
{
// Keep this in a static variable to make it accessible for all the nested activities, lets them manipulate the view
public static SettingsActivityGroup group;
// Need to keep track of the history if you want the back-button to work properly, don't use this if your activities requires a lot of memory.
private ArrayList<View> history;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Allocate history
this.history = new ArrayList<View>();
// Set group
group = this;
// Start root (first) activity
Intent myIntent = new Intent(this, SettingsActivity.class); // Change to the first activity of your ActivityGroup
myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
ReplaceView("SettingsActivity", myIntent);
}
/*
* Replace the activity with a new activity and add previous one to history
*/
public void ReplaceView(String pId, Intent pIntent)
{
Window window = getLocalActivityManager().startActivity(pId, pIntent);
View view = (window != null) ? window.getDecorView() : null;
// Add the old activity to the history
history.add(view);
// Set content view to new activity
setContentView(view);
}
/*
* Go back from previous activity or close application if there is no previous activity
*/
public void back()
{
if(history.size() > 1)
{
// Remove previous activity from history
history.remove(history.size()-1);
// Go to activity
View view = history.get(history.size() - 1);
Activity activity = (Activity) view.getContext();
// "Hack" used to determine when going back from a previous activity
// This is not necessary, if you don't need to redraw an activity when going back
activity.onWindowFocusChanged(true);
// Set content view to new activity
setContentView(view);
}
else
{
// Close the application
finish();
}
}
/*
* Overwrite the back button
*/
@Override
public void onBackPressed()
{
// Go one back, if the history is not empty
// If history is empty, close the application
SettingsActivityGroup.group.back();
return;
}
}
因为我相信调用当前所选活动的onBackPressed()是理想的行为
另外值得注意的是,ActivityGroup已被弃用,但我假设您是在编写代码,因为其中没有任何奇怪之处。如果活动表示有兴趣处理此问题,则应处理此问题。else控件将被传递给活动组,该活动组是活动的父级。这是真的。我的目标是2.2,虽然我开始考虑使用支持库,但我认为将ActivityGroup转换为片段太多了。我在childActivity中覆盖了onBackPressed(),使应用程序切换到上一个活动。不幸的是,该活动似乎也“不活动”,这意味着我可以看到视图中的所有内容,但不能按任何内容。切换到另一个选项卡,然后再次切换回“启用”活动。想法?用你的代码编辑你的问题,这样我或其他人就有机会发现问题。好了。我真的很感谢你的帮助。我已经被困在这里好几天了。希望这个建议能帮你解决这个问题,我想这一定是活动没有得到关注的问题。
public class CallForwardActivity extends ListActivity
{
....
@Override
public void onBackPressed()
{
// Go one back, if the history is not empty
// If history is empty, close the application
SettingsActivityGroup.group.back();
return;
}
}