Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to use the FragmentTransaction's function "addToBackStack(String string)"
    primarykey
    data
    text
    <p>I want to comply a Tab in my application.So,I use a TabHost(within three fragment). In tab two or three ,I want to come to tab one instend of destroy the Acticity when I click the "Back" button on my phone.</p> <p>Can "addToBackStack(String temp)" comply it instend of doing something in the function of "onBackPressed".</p> <p>Below is my code. ps:This is my first question.My English is soso,sorry.</p> <pre><code>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); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload