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.
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
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.