2013-12-14 74 views
0

我想遵守我的應用程序中的一個標籤。因此,我使用TabHost(在三個片段內)。 在標籤二或三,當我點擊我的手機上的「後退」按鈕時,我想要選擇一個破壞Acticity的插件。如何使用FragmentTransaction函數「addToBackStack(String string)」

「addToBackStack(String temp)」是否符合 「onBackPressed」的功能。

以下是我的代碼。 ps:這是我的第一個問題。我的英文是soso,對不起。

package in.wptrafficanalyzer.navigationtabdemo; 
import android.app.Activity; 
import android.app.Fragment; 
import android.app.FragmentManager; 
import android.app.FragmentTransaction; 
import android.os.Bundle; 
import android.view.GestureDetector; 
import android.view.MotionEvent; 
import android.widget.TabHost; 
public class MainActivity extends Activity{ 

private static final int SWIPE_MIN_DISTANCE = 120; 
private static final int SWIPE_MAX_OFF_PATH = 250; 
private static final int SWIPE_THRESHOLD_VELOCITY = 200; 
private TabHost tHost; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    tHost = (TabHost) findViewById(R.id.tabhost1); 
    tHost.setup(); 

    TabHost.OnTabChangeListener tabChangeListener = new TabHost.OnTabChangeListener() 
    { 
     @Override 
     public void onTabChanged(String tabId) 
     { 
      FragmentManager fm = getFragmentManager(); 
      Fragment androidFragment = fm.findFragmentByTag("android"); 
      Fragment appleFragment = fm.findFragmentByTag("apple"); 
      Fragment wpFragment = fm.findFragmentByTag("wp"); 
      FragmentTransaction ft = fm.beginTransaction(); 

      if(androidFragment!=null) 
       ft.detach(androidFragment); 
      if(appleFragment!=null) 
       ft.detach(appleFragment); 
      if(wpFragment != null) 
       ft.detach(wpFragment); 
      /** If current tab is android */ 
      if(tabId.equalsIgnoreCase("tabAndroid")) 
      { 
       if(androidFragment==null)  
        /** Create AndroidFragment and adding to fragmenttransaction */ 
        ft.replace(R.id.realtabcontent, new AndroidFragment(), "android"); 
       else 
        /** Bring to the front, if already exists in the fragmenttransaction */ 
        ft.attach(androidFragment); 
       if(fm.getBackStackEntryCount() == 0) 
       { 
        ft.addToBackStack(null); 
       } 
      } 
      else if(tabId.equalsIgnoreCase("tabApple")) 
      { 
       /** If current tab is apple */ 
       if(appleFragment==null) 
        /** Create AppleFragment and adding to fragmenttransaction */ 
        ft.replace(R.id.realtabcontent,new AppleFragment(), "apple");      
       else 
        /** Bring to the front, if already exists in the fragmenttransaction */ 
        ft.attach(appleFragment); 
      } 
      else 
       if(wpFragment==null) 
        ft.replace(R.id.realtabcontent,new WPFragment(), "wp"); 
       else 
        ft.attach(wpFragment); 
      ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); 
      ft.commit();     
     } 
    }; 
    /** Setting tabchangelistener for the tab */ 
    tHost.setOnTabChangedListener(tabChangeListener); 

    /** Defining tab builder for Andriod tab */ 
    TabHost.TabSpec tSpecAndroid = tHost.newTabSpec("tabAndroid"); 
    tSpecAndroid.setIndicator("Android",null);   
    tSpecAndroid.setContent(new DummyTabContent(getBaseContext())); 
    tHost.addTab(tSpecAndroid); 

    /** Defining tab builder for Apple tab */ 
    TabHost.TabSpec tSpecApple = tHost.newTabSpec("tabApple"); 
    tSpecApple.setIndicator("Apple",null);   
    tSpecApple.setContent(new DummyTabContent(getBaseContext())); 
    tHost.addTab(tSpecApple); 

    TabHost.TabSpec tSpecWp = tHost.newTabSpec("tabWp"); 
    tSpecWp.setIndicator("Wp",null);   
    tSpecWp.setContent(new DummyTabContent(getBaseContext())); 
    tHost.addTab(tSpecWp); 
} 
} 

回答

1

你可以做這樣的事情:

@Override 
     public void onBackPressed() { 
      // TODO Auto-generated method stub 




     if (getSupportFragmentManager().getBackStackEntryCount() > 0) { 
        fragmentManager.popBackStack(null, 
           FragmentManager.POP_BACK_STACK_INCLUSIVE); 
       } 
     else{ 
     super.onBackPressed(); 
     } 

     } 

如果你的籌碼是不是空,檢查後退按鈕,讓它空,如果是空出去

+0

這不可能成功地工作。 tabhost不能隨片段而改變。也許「addToBackStack」在TabHost無法正常工作? 「tabHost.setCurrentTab()」可以工作。 –

相關問題