快速问题:我有一个activitygroup。在该活动组中,我有一项活动。如果我在这个活动中按回来。该活动的onBackPressed方法被称为 - 不是活动组onBackPressed - 为什么?onBackPressed()行为与ActivityGroups
编辑:得到了我的答案,但问题依然存在。这里有我的原始问题的代码和解释:
我在一个TabHost中使用ActivityGroups,因此已被“强制”到重写onBackPressed。通过按下我的手机并按下我的tabhost上的选项卡,我可以轻松浏览我的应用程序。但按Back键后我无法与界面进行交互。 一旦我再次按下tabhost上的其中一个选项卡,我就可以像正常一样进行交互。这是为什么发生?我需要重写onResume吗?
相关代码
SettingsActivityGroup:
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;
}
}
SettingsActivityGroup任意子(CallForwardActivity)
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;
}
}
没有什么奇怪的。活动应该处理它,如果它表示有兴趣处理这个。否则控制会通过作为活动父项的活动组。 – jeet 2012-02-29 13:16:37