10

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

this happens in the code here

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
    }
}

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?


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

JJD
  • 44,755
  • 49
  • 183
  • 309
Metallicraft
  • 2,201
  • 5
  • 30
  • 50

3 Answers3

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
        }
    }
Metallicraft
  • 2,201
  • 5
  • 30
  • 50
2

if you are using the FragmentTransaction in API 4+ compatablity package -android.support.v4.app.FragmentTransaction - you should be able to call attach and detach methods on pre API level 13 platforms.

http://developer.android.com/resources/samples/Support4Demos/index.html

Henok T
  • 784
  • 10
  • 5
  • Related blog post: [Fragments for all](http://android-developers.blogspot.com/2011/03/fragments-for-all.html) – blahdiblah Feb 07 '13 at 02:46
1

or just use http://actionbarsherlock.com/ you will have consistent action bar experience from 2.x up

AndroidGecko
  • 13,296
  • 3
  • 31
  • 47