使用要显示上一个活动的活动组在选项卡之间切换

时间:2011-08-16 11:35:34

标签: android

我的问题是在使用要显示上一个活动的活动组的标签之间切换。 当我们浏览选项卡时,我想显示上次打开/访问的屏幕。我的第一个屏幕是:

这是我的maninActivity'

    public class MainActivity extends TabActivity {
    int selectedTab;
    TabHost tabHost ;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tabview);

        TabHost t = getTabHost();
        tabHost = (TabHost)findViewById(android.R.id.tabhost);
        Resources res = getResources();
        TabSpec firstTabSpec = tabHost.newTabSpec("tid1");
        TabSpec secondTabSpec = tabHost.newTabSpec("tid1");
        TabSpec thirdTabSpec = tabHost.newTabSpec("tid1");
        TabSpec fouthTabSpec = tabHost.newTabSpec("tid1");
        /** TabSpec setIndicator() is used to set name for the tab. */
        /** TabSpec setContent() is used to set content for a particular tab. */
        firstTabSpec.setIndicator("Sales",res.getDrawable(R.drawable.ic_tab_artists_grey)).setContent(new Intent(this,SalesActivityGroup.class));
        secondTabSpec.setIndicator("Admin",res.getDrawable(R.drawable.admin)).setContent(new Intent(this,SettingActivityGroup.class));
        thirdTabSpec.setIndicator("Setting",res.getDrawable(R.drawable.ic_tab_artists_grey)).setContent(new Intent(this,SettingActivityGroup.class));
        fouthTabSpec.setIndicator("Inquiry",res.getDrawable(R.drawable.ic_tab_artists_grey)).setContent(new Intent(this,SettingActivityGroup.class));

        tabHost.addTab(firstTabSpec);
        tabHost.addTab(secondTabSpec);
        tabHost.addTab(thirdTabSpec);
        tabHost.addTab(fouthTabSpec);
        tabHost.setCurrentTab(0);
        tabHost.setMinimumHeight(18);
        tabHost.setFadingEdgeLength(5);

    }

    public void onTabChanged(String arg0) {
            selectedTab = tabHost.getCurrentTab();

    }
}

这是我的SalesActivityGroup

    public class SalesActivityGroup extends ActivityGroup {

    public static SalesActivityGroup group;
    private ArrayList<View> history;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.history = new ArrayList<View>();
        group = this;

        View view = getLocalActivityManager().startActivity("Sales",
                new Intent(this, SalesRouteActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
                .getDecorView();

        replaceView(view);

    }

    public void replaceView(View v) {
        history.add(v);
        setContentView(v);

    }

    public void back() {
        if (history.size() > 0) {
            history.remove(history.size() - 1);
            if (history.size() > 0) {
                setContentView(history.get(history.size() - 1));
            } else {
                finish();
            }
        } else {
            finish();
        }
    }

    public void backToFirst() {
        int size = history.size();
        while (size > 1) {
            history.remove(size - 1);
            size = history.size();
        }
        setContentView(history.get(0));
    }

    @Override
    public void onBackPressed() {
        SalesActivityGroup.group.back();
        return;
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    //  super.onActivityResult(requestCode, resultCode, data);
        Log.i("****" , "requestCode" + requestCode);
        Bundle bundle = data.getExtras();
        String roteCode = bundle.getString("RouteCode");
        Intent intent = new Intent(SalesActivityGroup.this,ListRetailerActivity.class);
        bundle.putString("RouteCode", roteCode);
        intent.putExtras(bundle);
        View view = SalesActivityGroup.group.getLocalActivityManager()
                .startActivity("",intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView();
        SalesActivityGroup.group.replaceView(view);
        }


}

这是我在SalesRouteActivity中的调用部分

Intent intent = new Intent(SalesRouteActivity.this, ListRetailerActivity.class);
                        Bundle bundle = new Bundle();
                        bundle.putString("RouteName", keyword);
                        intent.putExtras(bundle);
            View view = SalesActivityGroup.group.getLocalActivityManager().startActivity("", intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView();  
            SalesActivityGroup.group.replaceView(view);

与上面的代码相同,我为下一个标签设置了Activity Activity

public class SettingActivityGroup extends ActivityGroup {

    // Keep this in a static variable to make it accessible for all the nested
    // activities, lets them manipulate the view
    public static SettingActivityGroup 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);
        this.history = new ArrayList<View>();
        group = this;

        // Start the root activity withing the group and get its view
        View view = getLocalActivityManager().startActivity(
                "Setting",
                new Intent(this, SettingScreenActivity.class)
                .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
                .getDecorView();

        // Replace the view of this ActivityGroup
        replaceView(view);

    }

    public void replaceView(View v) {
        // Adds the old one to history
        history.add(v);
        // Changes this Groups View to the new View.
        setContentView(v);

    }


    public void back() {
        if (history.size() > 0) {
            history.remove(history.size() - 1);
            if (history.size() > 0) {
                setContentView(history.get(history.size() - 1));
            } else {
                finish();
            }
        } else {
            finish();
        }
    }

    @Override
    public void onBackPressed() {
        SettingActivityGroup.group.back();
        return;
    }

}

我在这里粘贴了我的代码。 http://pastebin.com/D4fvkGBx

我在这方面遇到麻烦......

请帮帮我

我的代码出错了吗?

提前致谢

3 个答案:

答案 0 :(得分:0)

您需要覆盖

onBackPressed 

并通过处理后退按钮设置您要执行的操作。例如,您可以在用户按下时显示上一个选项卡或您想要的某个默认选项卡。

答案 1 :(得分:0)

在创建的metoth中

保存您使用的选项卡:)

int lastTab;
@Override public void onBackPressed() { 
        tabHost.setCurrentTab(lastTab);
        tabHost.setMinimumHeight(18);
        tabHost.setFadingEdgeLength(5);return; }

答案 2 :(得分:0)

我的MainActivity类中存在一些问题

  tabHost = getTabHost();
    TabHost.TabSpec spec;
    Intent intent;

    intent = new Intent().setClass(this, SalesActivityGroup.class);  
    spec = getTabHost().newTabSpec("Sales").setIndicator("Sales",getResources().getDrawable(R.drawable.sales )).setContent(intent);
    tabHost.addTab(spec);

    intent = new Intent().setClass(this, SettingActivityGroup.class);  
    spec = getTabHost().newTabSpec("Admin").setIndicator("Admin",getResources().getDrawable(R.drawable.admin1)).setContent(intent);
    tabHost.addTab(spec);

    intent = new Intent().setClass(this, SettingActivityGroup.class);  
    spec = getTabHost().newTabSpec("Setting").setIndicator("Setting",getResources().getDrawable(R.drawable.maintenance)).setContent(intent);
    tabHost.addTab(spec);

    intent = new Intent().setClass(this, SettingActivityGroup.class);  
    spec = getTabHost().newTabSpec("Inquiry").setIndicator("Inquiry",getResources().getDrawable(R.drawable.inquiry)).setContent(intent);
    tabHost.addTab(spec);

    tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundResource(R.drawable.selected_tab);


    tabHost.setCurrentTab(0);
    tabHost.setMinimumHeight(18);
    tabHost.setFadingEdgeLength(5);
    tabHost.setFocusable(true); 
    tabHost.requestFocus();
    tabHost.setFadingEdgeLength(5);

无需在此处调用findById()。

错误tabHost = (TabHost)findViewById(android.R.id.tabhost);