-1

我必须从 TabActivity 的 5 个片段中修改几个布局的后退按钮命令。我为主要活动尝试了这段代码:

@Override
    public void onTabChanged(String tabId) {
        TabInfo newTab = mTabs.get(tabId);
                    if(mTabHost.getCurrentTab() == 0){
                 backpag=0;
            }
        if (mTabHost.getCurrentTab() == 1) {
            backpag=1;
        }
        if (mTabHost.getCurrentTab() == 2) {
            backpag=2;
        }
        if (mTabHost.getCurrentTab() == 3) {
            backpag=3;
        }
        if (mTabHost.getCurrentTab() == 4) {
            backpag=4;
        }
        if (mTabHost.getCurrentTab() == 5) {
            backpag=5;
        }
        if (mLastTab != newTab) {
            FragmentTransaction ft = mActivity.getSupportFragmentManager().beginTransaction();
            if (mLastTab != null) {
                if (mLastTab.fragment != null) {
                    ft.detach(mLastTab.fragment);
                }
            }
            if (newTab != null) {
                if (newTab.fragment == null) {
                    newTab.fragment = Fragment.instantiate(mActivity, newTab.clss.getName(), newTab.args);
                    ft.add(mContainerId, newTab.fragment, newTab.tag);
                } else {
                    ft.attach(newTab.fragment);
                }
            }

            mLastTab = newTab;
            ft.commit();
            mActivity.getSupportFragmentManager().executePendingTransactions();
        }

    }
}

  @Override
public void onBackPressed() {

    System.out.println("mtabhost" + mTabHost.getCurrentTab());
    if (backpag == 1) {
        VinocardFragment.onBackPressed();
    } else if (backpag == 2) {
        StandorteFragment.getInstance().onBackPressedStandorte();
    } else if (backpag == 3) {
        // StandorteFragment.onBackPressed();
    } else if (backpag == 4) {
        // StandorteFragment.onBackPressed();
    } else if (backpag == 5) {
        SonstigesFragment.onBackPressed();
    } else if (backpag == 0) {
        System.out.println("inainte d getinstance");
        LoggedFragment.getInstance().onBackPressedLogged();
        backpag=-1;
        System.out.println("mtabhost" + backpag);
    } else if(backpag==-1) {
        System.out.println("mtabhost" + backpag);
        finish();
    }
}

但是当我到达第一个选项卡(backpag = 0)时,我按下后退按钮并开始一个循环,就像我只需按下后退按钮一样,它会转到第一个选项卡和它之前的选项卡。在我的逻辑中,我按下后退按钮形成它应该退出的第一个选项卡(完成())。任何想法为什么会发生这种情况?

4

1 回答 1

0

用这个管理:

public void onBackPressed() {

        // super.onBackPressed();

        Log.d("debug", "tab back pressed :: " + backpag);
        if (backpag == 0) {
            SharedPreferences settings = getSharedPreferences("PrefFile", 0);
            SharedPreferences.Editor editor = settings.edit();
            editor.putBoolean("backfromtab", true);
            editor.commit();
            Intent back2main = new Intent(this, WeinCoMainActivity.class);
            startActivity(back2main);
        } else if (backpag == 1) {
            VinocardFragment.getInstance().onBackPressedVinocard();
        } else if (backpag == 2) {
            StandorteFragment.getInstance().onBackPressedStandorte();
        } else if (backpag == 3) {
            // webshop
        } else if (backpag == 4) {
            // scanner
        } else if (backpag == 5) {
            SonstigesFragment.getInstance().onBackPressedSonstiges();
        }
    }
于 2012-08-10T14:23:51.067 回答