Android的TabActivity后退按钮功能与多个子活动 [英] Android TabActivity Back Button Functionality with Multiple Child Activities

查看:97
本文介绍了Android的TabActivity后退按钮功能与多个子活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有TabActivity在Android项目,其中包含一些标签。在每个标签,我可以打开各种活动,后在标签中打开它,我想回到同一标签previous活动,但默认的Andr​​oid行为关闭我的根标签的活动。我如何能实现的行为,我需要?

i have TabActivity in android project which contains some tabs. In each tab i can open various activities, and after open it in a tab i want go back to previous activity in same tab, but default android behavior close my root tab activity. How i can realise behavior that i need?

推荐答案

有这样做的几种方法。首先需要创建一个自定义GroupActivity将跟踪堆叠从LocalActivityManager,然后扩展该类为每个标签页。对于这一点,看看这个教程:

There are a few ways of doing this. The first involves creating a custom GroupActivity that will keep track of the stack from the LocalActivityManager and then extending that class for each of your tabs. For that, check out this tutorial:

http://ericharlow.blogspot.com/2010/09/experience-multiple-android-activities.html

有一个简单的方法就是保持你的选项卡的子视图数组您最初的ActivityGroup类中,然后覆盖后退按钮。下面是一些示例code:

A simpler approach is to keep an array of your tab's subviews within your initial ActivityGroup class and then override the back button. Here's some sample code:

public void replaceContentView(String id, Intent newIntent) {
    View view = getLocalActivityManager()
                    .startActivity(id, newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)) 
                    .getDecorView();
    viewList.add(view); // Add id to keep track of stack.
    this.setContentView(view);
}       


public void previousView() {

    if(viewList.size() > 0) {  
        viewList.remove(viewList.size()-1);
        if (viewList.size() > 0)
            setContentView(viewList.get(viewList.size()-1)); 
        else
          initView();
    }else {  
        finish();  
    }  
}

在initView()类包含所有原始活动的意见的充气的。这样,您就可以调用此方法来再现原始的活动,如果有数组中没有更多的意见。

The initView() class holds all of the inflating of the original activity's view. This way, you can call this method to regenerate the original activity if there are no more views in the array.

这篇关于Android的TabActivity后退按钮功能与多个子活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆