FragmentTransaction .attach和.detach的动作条的标签 [英] FragmentTransaction .attach and .detach for Actionbar tabs

查看:385
本文介绍了FragmentTransaction .attach和.detach的动作条的标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让code <一个href="http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/FragmentTabs.html"相对=nofollow>这里工作。它编译罚款。它将运行。而且它会加载(3)选项卡1。然而,当我点击第二或第三选项卡上,我得到这样的:

I'm trying to get the code here to work. It compiles fine. It will run. And it will load tab 1 (of 3). However, when I click on the 2nd or 3rd tab, I get this:

java.lang.NoSuchMethodError:android.app.FragmentTransaction.detach

java.lang.NoSuchMethodError: android.app.FragmentTransaction.detach

这发生在code此处

public void onTabUnselected(Tab tab, FragmentTransaction ft) {
    if (mFragment != null) {
        //ft.detach(mFragment); //requires API Level 13
        ft.remove(mFragment); //this does not do the same thing as detach
    }
}

我发现,<一个href="http://developer.android.com/reference/android/app/FragmentTransaction.html#detach%28android.app.Fragment%29"相对=nofollow>分离是仅适用于API级别13.我试过<一个href="http://developer.android.com/reference/android/app/FragmentTransaction.html#remove%28android.app.Fragment%29"相对=nofollow>删除,但它不会做同样的事情,效果显着。有没有人有关于如何基于code在第一个<一个克服任何想法href="http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/FragmentTabs.html"相对=nofollow>链接?

I found that detach is only available to API Level 13. I tried remove, but it doesn't do the same thing, obviously. Does anyone have any ideas about how to overcome this based on the code in the first link?

编辑: 我想这同样适用<一个href="http://developer.android.com/reference/android/app/FragmentTransaction.html#attach%28android.app.Fragment%29"相对=nofollow>附加因为这也是在code,但没有得到应用程序崩溃之前打了。

I guess the same goes for attach as that's also in the code, but doesn't get hit before the app crashes.

推荐答案

看来,更换几个地方在code引用连接和分离有添加和删除将使code在正常一个pre-API级别13环境。

It appears that replacing the several places in the code that reference attach and detach with add and remove will allow the code to function normally in a pre-API Level 13 environment.

    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.remove(mFragment);
            ft.commit();
        }
    }

    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        //if (mFragment == null) {
            mFragment = Fragment.instantiate(mActivity, mClass.getName(), mArgs);
            ft.add(android.R.id.content, mFragment, mTag);
        //} else {
        //    ft.attach(mFragment);
        //}
    }

    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        if (mFragment != null) {
            //ft.detach(mFragment); //requires API 13
            ft.remove(mFragment); //this does not do the same thing as detach
        }
    }

这篇关于FragmentTransaction .attach和.detach的动作条的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆