0
내 응용 프로그램에서 Tab을 따르려고합니다. 따라서 TabHost (세 조각 내)를 사용합니다. 탭 2 ~ 3에서, 휴대 전화에서 "뒤로"버튼을 클릭하면 활동성을 파괴하라는 메시지가 나타납니다.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);
}
}
외출은 할 수 없습니다 성공적으로 작동합니다. 탭 호스트는 조각으로 변경할 수 없습니다. 어쩌면 "addToBackStack"잘 TabHost에서 작동하지 않을 수 ?? "tabHost.setCurrentTab()"가 작동 할 수 있습니다. –