2012-08-08 226 views
-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)我按下後退按鈕並開始一個循環,在我剛按下後退按鈕,它會轉到第一個選項卡和之前的選項卡。在我的邏輯後,我按下後退按鈕形式的第一個標籤,它應該退出(完成())。任何ideeas爲什麼發生這種情況?

+4

您可以在活動實施onBackPressed()調用的片段onBackPressed()方法。 – 2012-08-08 11:59:11

回答

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(); 
     } 
    }