0

Im a little bit confused by the support package android.support.v4.app because i implemented a class extending the android.support.v4.app.FragmentActivity and noticed that it has methods which are expecting android.app.FragmentTransaction as a parameter. Why arent they expecting android.support.v4.app.FragmentTransaction instead?

I still didnt get the difference between Fragment, DialogFragment, FragmentManager etc. from the support package compared to the regular packages. When do i have to use what?

I implemented for example a DialogFragment from the regular package and use it in all my acitvities when i have to make an alert to the user. Unfortunately now i have to write antother DialogFragment implementing the supertype from the support package, because i got some fragments which are using the implementations from the support package. I cant change them because they arent from me.

public class DiagramsActivity extends FragmentActivity implements TabListener, OnPageChangeListener {
    private ViewPager viewPager = null;
    private FragmentPagerAdapter fragmentPager = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        this.fragmentPager = new DiagramsFragmentPagerAdapter(this.getSupportFragmentManager());

        this.setContentView(R.layout.activity_diagrams); 

        this.viewPager = (ViewPager) this.findViewById(R.id.diagrams_pager);
        this.viewPager.setOnPageChangeListener(this);
        this.viewPager.setAdapter(this.fragmentPager);
        this.actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        this.actionBar.addTab(actionBar.newTab().setText(this.getResources().getString(R.string.text_line)).setTabListener(this));
        this.actionBar.addTab(actionBar.newTab().setText(this.getResources().getString(R.string.text_bar)).setTabListener(this));
        this.actionBar.addTab(actionBar.newTab().setText(this.getResources().getString(R.string.text_scatter)).setTabListener(this));
        this.actionBar.addTab(actionBar.newTab().setText(this.getResources().getString(R.string.text_pie)).setTabListener(this));
    }

    @Override
    public void onTabSelected(Tab tab, android.app.FragmentTransaction fragmentTransaction) {
        this.viewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(Tab tab, android.app.FragmentTransaction fragmentTransaction) {}

    @Override
    public void onTabReselected(Tab tab, android.app.FragmentTransaction fragmentTransaction) {}

    @Override
    public void onPageScrollStateChanged(int state) {}

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}

    @Override
    public void onPageSelected(int position) {
        this.actionBar.setSelectedNavigationItem(position);
    }

    private void performChooseDates() {
        FragmentTransaction fragmentTransaction = this.getSupportFragmentManager().beginTransaction();
        MeasureDataSelectionDialogFragment measureDataGetSelectionDialogFragment = new MeasureDataSelectionDialogFragment();

        measureDataGetSelectionDialogFragment.setCallback(this);
        measureDataGetSelectionDialogFragment.show(fragmentTransaction, null);
    }
}

The FragmentActivity is from the support package. The methods from the TabListener are expecting android.app.FragmentTransaction so does my method performChooseDates(). So i have an (from my point of view) ugly mix of support package and regular package. the Viewpager mentioned in the code and the FragmentPagerAdapter are also from the support package because they dont exist in the regular package.

Mulgard
  • 9,877
  • 34
  • 129
  • 232

1 Answers1

0

All the apps I write need to support right back to Android 2.3 and this is the easiest way to do it. If you're supporting 11+ then stick to android.app.Fragment.

android.support.v4.app.Fragment is the Fragment class in the android support library, which is a compatibility package that allows you to use some of the newer features of Android on older versions of Android. android.support.v4.app.Fragment is a library that you can use to get backwards-compatibility for older API version.

android.app.Fragment is the Fragment class in the native version of the Android SDK. It was introduced in API 11. If you want to make your app use fragments, and want to target devices before API 11, you must use android.support.v4.app.Fragment. If you're only targeting devices running API 11 or above, you can use android.app.Fragment.

Maveňツ
  • 1
  • 12
  • 50
  • 89
  • but some classes are just available using the support package arent they? i use vor example the class android.support.v4.view.ViewPager and i could not find a ViewPager in the regular package. – Mulgard Nov 25 '14 at 11:48
  • For that you have to add the Android Support Library check [this](http://stackoverflow.com/questions/14218519/want-to-use-viewpager-cannot-get-android-support-to-be-recognized) – Maveňツ Nov 25 '14 at 11:50
  • So i will get a mix of support package and regular package anyway even when i just support api level 13 and newer... – Mulgard Nov 25 '14 at 11:51
  • I dont get any errors. I was just asking about the differnece of those two packages and why i have to mix them together when i dont support apis lower than 13. i explained that i use viewpager for example, which is only present in the support package. so it seems that the support package is not only for supporting older apis. – Mulgard Nov 25 '14 at 12:05
  • [ViewPager](http://developer.android.com/reference/android/support/v4/view/ViewPager.html) was introduced in api 13+ so for using it for lower versions u need the `v4` jar – Maveňツ Nov 25 '14 at 12:08
  • but thats the point. i use 13+ but there is no ViewPager in another package than the support package. Its the same with FragmentPager. – Mulgard Nov 25 '14 at 12:10
  • Check this http://stackoverflow.com/questions/17553374/android-app-fragments-vs-android-support-v4-app-using-viewpager – Maveňツ Nov 25 '14 at 12:14