覆盖片段中的后退按钮

时间:2012-08-08 11:54:55

标签: android override fragment back-button tabactivity

我必须修改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)的那一刻,我按下后退按钮,它开始循环,就像我只需按下后退按钮,它就会转到第一个标签和前面的标签。在我按下后退按钮形成第一个标签后,我应该退出(finish())。任何想法为什么会发生这种情况?

1 个答案:

答案 0 :(得分: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();
        }
    }