下面列出了android.app.FragmentTransaction#detach ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void addTab(android.widget.TabHost.TabSpec tabspec, Class class1, Bundle bundle)
{
tabspec.setContent(new h(c));
String s = tabspec.getTag();
j j1 = new j(s, class1, bundle);
if (h)
{
j.a(j1, d.findFragmentByTag(s));
if (j.a(j1) != null && !j.a(j1).isDetached())
{
FragmentTransaction fragmenttransaction = d.beginTransaction();
fragmenttransaction.detach(j.a(j1));
fragmenttransaction.commit();
}
}
a.add(j1);
addTab(tabspec);
}
public void addTab(TabHost.TabSpec tabSpec, Class<?> clss, Bundle args) {
tabSpec.setContent(new DummyTabFactory(mContext));
String tag = tabSpec.getTag();
TabInfo info = new TabInfo(tag, clss, args);
if (mAttached) {
// If we are already attached to the window, then check to make
// sure this tab's fragment is inactive if it exists. This shouldn't
// normally happen.
info.fragment = mFragmentManager.findFragmentByTag(tag);
if (info.fragment != null && !info.fragment.isDetached()) {
FragmentTransaction ft = mFragmentManager.beginTransaction();
ft.detach(info.fragment);
ft.commit();
}
}
mTabs.add(info);
addTab(tabSpec);
}
public TabListener(Activity activity, String tag, Class<T> clz, Bundle args) {
mActivity = activity;
mTag = tag;
mClass = clz;
mArgs = args;
// Check to see if we already have a fragment for this tab, probably
// from a previously saved state. If so, deactivate it, because our
// initial state is that a tab isn't shown.
mFragment = mActivity.getFragmentManager().findFragmentByTag(mTag);
if (mFragment != null && !mFragment.isDetached()) {
FragmentTransaction ft = mActivity.getFragmentManager().beginTransaction();
ft.detach(mFragment);
ft.commit();
}
}
protected void onAttachedToWindow()
{
super.onAttachedToWindow();
String s = getCurrentTabTag();
FragmentTransaction fragmenttransaction = null;
int i = 0;
while (i < a.size())
{
j j1 = (j)a.get(i);
j.a(j1, d.findFragmentByTag(j.b(j1)));
if (j.a(j1) != null && !j.a(j1).isDetached())
{
if (j.b(j1).equals(s))
{
g = j1;
} else
{
if (fragmenttransaction == null)
{
fragmenttransaction = d.beginTransaction();
}
fragmenttransaction.detach(j.a(j1));
}
}
i++;
}
h = true;
FragmentTransaction fragmenttransaction1 = a(s, fragmenttransaction);
if (fragmenttransaction1 != null)
{
fragmenttransaction1.commit();
d.executePendingTransactions();
}
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
String currentTab = getCurrentTabTag();
// Go through all tabs and make sure their fragments match
// the correct state.
FragmentTransaction ft = null;
for (int i=0; i<mTabs.size(); i++) {
TabInfo tab = mTabs.get(i);
tab.fragment = mFragmentManager.findFragmentByTag(tab.tag);
if (tab.fragment != null && !tab.fragment.isDetached()) {
if (tab.tag.equals(currentTab)) {
// The fragment for this tab is already there and
// active, and it is what we really want to have
// as the current tab. Nothing to do.
mLastTab = tab;
} else {
// This fragment was restored in the active state,
// but is not the current tab. Deactivate it.
if (ft == null) {
ft = mFragmentManager.beginTransaction();
}
ft.detach(tab.fragment);
}
}
}
// We are now ready to go. Make sure we are switched to the
// correct tab.
mAttached = true;
ft = doTabChanged(currentTab, ft);
if (ft != null) {
ft.commit();
mFragmentManager.executePendingTransactions();
}
}
private FragmentTransaction doTabChanged(String tabId, FragmentTransaction ft) {
TabInfo newTab = null;
for (int i=0; i<mTabs.size(); i++) {
TabInfo tab = mTabs.get(i);
if (tab.tag.equals(tabId)) {
newTab = tab;
}
}
if (newTab == null) {
throw new IllegalStateException("No tab known for tag " + tabId);
}
if (mLastTab != newTab) {
if (ft == null) {
ft = mFragmentManager.beginTransaction();
}
if (mLastTab != null) {
if (mLastTab.fragment != null) {
ft.detach(mLastTab.fragment);
}
}
if (newTab != null) {
if (newTab.fragment == null) {
newTab.fragment = Fragment.instantiate(mContext,
newTab.clss.getName(), newTab.args);
ft.add(mContainerId, newTab.fragment, newTab.tag);
} else {
ft.attach(newTab.fragment);
}
}
mLastTab = newTab;
}
return ft;
}
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
if (mFragment != null) {
ft.setCustomAnimations(android.R.animator.fade_in,
R.animator.test);
ft.detach(mFragment);
}
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
public void detach(Fragment fragment,
FragmentTransaction currTransaction) {
currTransaction.detach(fragment);
}
private FragmentTransaction a(String s, FragmentTransaction fragmenttransaction)
{
j j1 = null;
int i = 0;
while (i < a.size())
{
j j2 = (j)a.get(i);
if (!j.b(j2).equals(s))
{
j2 = j1;
}
i++;
j1 = j2;
}
if (j1 == null)
{
throw new IllegalStateException((new StringBuilder()).append("No tab known for tag ").append(s).toString());
}
if (g != j1)
{
if (fragmenttransaction == null)
{
fragmenttransaction = d.beginTransaction();
}
if (g != null && j.a(g) != null)
{
fragmenttransaction.detach(j.a(g));
}
if (j1 != null)
{
if (j.a(j1) == null)
{
j.a(j1, Fragment.instantiate(c, j.c(j1).getName(), j.d(j1)));
fragmenttransaction.add(e, j.a(j1), j.b(j1));
} else
{
fragmenttransaction.attach(j.a(j1));
}
}
g = j1;
}
return fragmenttransaction;
}
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
if (mFragment != null) {
// Detach the fragment, because another one is being attached
ft.detach(mFragment);
}
}
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
if (mFragment != null) {
ft.detach(mFragment);
}
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
public void detach(Fragment fragment,
FragmentTransaction currTransaction) {
currTransaction.detach(fragment);
}