2012-02-29 23 views
0

快速问题:我有一个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; 
    } 

} 
+0

没有什么奇怪的。活动应该处理它,如果它表示有兴趣处理这个。否则控制会通过作为活动父项的活动组。 – jeet 2012-02-29 13:16:37

回答

2

,因为我相信调用onBackPressed当前选定的活动()是所期望的行为。

还值得注意的是,ActivityGroup已被弃用,但我认为你编码为< 3.0,并且不喜欢与支持库一起工作。

关于你编辑的问题: 这个网站的其他问题,引用这篇文章是一个很好的ActivityGroup例子,我会同意http://ericharlow.blogspot.com/2010/09/experience-multiple-android-activities.html 这个例子只是调用完成()上时被压回的当前活动,并且让OS重新开始之前的活动,这比您正在做的更简单,并且希望能够工作!你可以在你的子活动中调用getParent()来避免使用这个静态引用(这样看起来更容易阅读!)。

+0

这是真的。我的目标是2.2,尽管我开始考虑使用支持库,但我决定将ActivityGroups转换为Fragments的工作量太大。 – CodePrimate 2012-02-29 13:27:18

+0

现在我拥有了你。我在我的childActivity中覆盖了onBackPressed(),这使应用程序切换到上一个活动。不幸的是,这项活动似乎“无效”,这意味着我可以在视图中看到所有内容,但无法按任何内容。切换到另一个选项卡,然后再次返回“启用”该活动。思考? – CodePrimate 2012-02-29 13:29:06

+0

用您的代码编辑您的问题,以便我或其他人有机会发现问题。 – TheLastBert 2012-02-29 14:00:02