Android ActionBar Sherlock Tabs最初显示另一个选项卡';在加载其自己的内容之前,先删除其内容

Android ActionBar Sherlock Tabs最初显示另一个选项卡';在加载其自己的内容之前,先删除其内容,android,android-listview,android-actionbar,actionbarsherlock,Android,Android Listview,Android Actionbar,Actionbarsherlock,我在标签和ActionBarSherlock上有点麻烦 基本上,我有一个菜单,有3个按钮,加载一个单一的活动。此活动是Sherlocklist活动,有3个选项卡,将根据菜单中用于启动活动的项目进行选择 我遇到的问题是,当我通过菜单加载第一个选项卡时,返回菜单,然后从菜单加载第三个选项卡,它将显示第一个选项卡为选中状态,然后加载第三个选项卡 以下是有关我的实施的一些细节: 活动是: public class MyActivity extends SherlockListActivity imple

我在标签和ActionBarSherlock上有点麻烦

基本上,我有一个菜单,有3个按钮,加载一个单一的活动。此活动是Sherlocklist活动,有3个选项卡,将根据菜单中用于启动活动的项目进行选择

我遇到的问题是,当我通过菜单加载第一个选项卡时,返回菜单,然后从菜单加载第三个选项卡,它将显示第一个选项卡为选中状态,然后加载第三个选项卡

以下是有关我的实施的一些细节:

活动是:

public class MyActivity extends SherlockListActivity implements ActionBar.TabListener
    String dToken = null;

    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        dToken = extras.getString("value");
    }
    mode = dToken;
    ActionBar ab = getSupportActionBar();
    ab.setDisplayHomeAsUpEnabled(true);

    //Sets the ActionBarSherlock Tabs
    getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    ActionBar.Tab leftTab = getSupportActionBar().newTab();
    leftTab.setText("First");
    leftTab.setTabListener(this);

    ActionBar.Tab midTab = getSupportActionBar().newTab();
    midTab.setText("Second");
    midTab.setTabListener(this);

    ActionBar.Tab rightTab = getSupportActionBar().newTab();
    rightTab.setText("Third");
    rightTab.setTabListener(this);
    if (mode.equals("first"))
        ab.addTab(leftTab, true);
    else ab.addTab(leftTab, false);             
    if (mode.equals("second"))
        ab.addTab(midTab, true);
    else ab.addTab(midTab, false); 
    if (mode.equals("third"))
        ab.addTab(rightTab, true);
    else ab.addTab(rightTab, false);
在onCreate中调用以下命令以设置名为mode的全局字符串:

public class MyActivity extends SherlockListActivity implements ActionBar.TabListener
    String dToken = null;

    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        dToken = extras.getString("value");
    }
    mode = dToken;
    ActionBar ab = getSupportActionBar();
    ab.setDisplayHomeAsUpEnabled(true);

    //Sets the ActionBarSherlock Tabs
    getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    ActionBar.Tab leftTab = getSupportActionBar().newTab();
    leftTab.setText("First");
    leftTab.setTabListener(this);

    ActionBar.Tab midTab = getSupportActionBar().newTab();
    midTab.setText("Second");
    midTab.setTabListener(this);

    ActionBar.Tab rightTab = getSupportActionBar().newTab();
    rightTab.setText("Third");
    rightTab.setTabListener(this);
    if (mode.equals("first"))
        ab.addTab(leftTab, true);
    else ab.addTab(leftTab, false);             
    if (mode.equals("second"))
        ab.addTab(midTab, true);
    else ab.addTab(midTab, false); 
    if (mode.equals("third"))
        ab.addTab(rightTab, true);
    else ab.addTab(rightTab, false);
ABS和制表符在onCreate中使用以下内容初始化:

public class MyActivity extends SherlockListActivity implements ActionBar.TabListener
    String dToken = null;

    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        dToken = extras.getString("value");
    }
    mode = dToken;
    ActionBar ab = getSupportActionBar();
    ab.setDisplayHomeAsUpEnabled(true);

    //Sets the ActionBarSherlock Tabs
    getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    ActionBar.Tab leftTab = getSupportActionBar().newTab();
    leftTab.setText("First");
    leftTab.setTabListener(this);

    ActionBar.Tab midTab = getSupportActionBar().newTab();
    midTab.setText("Second");
    midTab.setTabListener(this);

    ActionBar.Tab rightTab = getSupportActionBar().newTab();
    rightTab.setText("Third");
    rightTab.setTabListener(this);
    if (mode.equals("first"))
        ab.addTab(leftTab, true);
    else ab.addTab(leftTab, false);             
    if (mode.equals("second"))
        ab.addTab(midTab, true);
    else ab.addTab(midTab, false); 
    if (mode.equals("third"))
        ab.addTab(rightTab, true);
    else ab.addTab(rightTab, false);
现在设置选择哪个选项卡(使用从上面第二块代码中获得的模式变量),这是onCreate:

public class MyActivity extends SherlockListActivity implements ActionBar.TabListener
    String dToken = null;

    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        dToken = extras.getString("value");
    }
    mode = dToken;
    ActionBar ab = getSupportActionBar();
    ab.setDisplayHomeAsUpEnabled(true);

    //Sets the ActionBarSherlock Tabs
    getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    ActionBar.Tab leftTab = getSupportActionBar().newTab();
    leftTab.setText("First");
    leftTab.setTabListener(this);

    ActionBar.Tab midTab = getSupportActionBar().newTab();
    midTab.setText("Second");
    midTab.setTabListener(this);

    ActionBar.Tab rightTab = getSupportActionBar().newTab();
    rightTab.setText("Third");
    rightTab.setTabListener(this);
    if (mode.equals("first"))
        ab.addTab(leftTab, true);
    else ab.addTab(leftTab, false);             
    if (mode.equals("second"))
        ab.addTab(midTab, true);
    else ab.addTab(midTab, false); 
    if (mode.equals("third"))
        ab.addTab(rightTab, true);
    else ab.addTab(rightTab, false);
假设在菜单中,我单击第一个菜单项。它将加载第一个选项卡的结果。我返回菜单并单击第三个菜单项,它将加载第一个选项卡的结果几秒钟,然后切换到第三个选项卡的结果

当每个菜单项加载单独的活动时,这不是问题,但我合并了它们,因为三个活动之间的代码几乎相同


每当使用“后退”按钮时,我都会尝试调用this.finish,这样我就可以清除活动,但没有成功。

看看我的选项卡侦听器

private class MyTabListener implements ActionBar.TabListener {
    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {

        switch (tab.getPosition()) {
        case 0:

            if (frag1 == null) {
                // If not, instantiate and add it to the activity
                frag1 = Fragment.instantiate(getApplicationContext(),
                        PackagesFragment.class.getName());
                ft.add(android.R.id.content, frag1, "Feeds");
            } else {
                // If it exists, simply attach it in order to show it
                ft.show(frag1);
            }
            return;

        case 1:

            if (frag2 == null) {
                // If not, instantiate and add it to the activity
                frag2 = Fragment.instantiate(getApplicationContext(),
                        BlogsFragment.class.getName());
                ft.add(android.R.id.content, frag2, "Profile");
            } else {
                // If it exists, simply attach it in order to show it
                ft.show(frag2);
            }
            return;
        case 2:

            if (frag3 == null) {
                // If not, instantiate and add it to the activity
                frag3 = Fragment.instantiate(getApplicationContext(),
                        GalleryFragment.class.getName());
                ft.add(android.R.id.content, frag3, "Gallery");
            } else {
                // If it exists, simply attach it in order to show it
                ft.show(frag3);
            }
            return;

        }

    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub

            // Detach the fragment, because another one is being attached
            switch (tab.getPosition()) {
            case 0:
                ft.hide(frag1);
                return;
            case 1:
                ft.hide(frag2);
                return;
            case 2:
                ft.hide(frag3);
                return;


            }


    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub
    }
}