0

I have faced problem while creating fragments. In my mainActivity i am calling this fragment..

Fragment fr = new Article();

FragmentManager fm = getFragmentManager();

FragmentTransaction fragmentTransaction = fm.beginTransaction();

fragmentTransaction.replace(R.id.container, fr);

fragmentTransaction.addToBackStack(null);

fragmentTransaction.commit();

but it is not displaying because instead of given two imports I have to add the supprot.v4 one and

import android.app.FragmentManager;//correct one
import android.app.FragmentTransaction;

import android.support.v4.app.FragmentManager;// wrong one..
import android.support.v4.app.FragmentTransaction;

and in the fragment class i have to import this

import android.app.Fragment;
//instead of the given below..
import android.support.v4.app.Fragment;

Know the problem is solved.. But i am curious about the difference between them. And what is the better way of using fragment. I have created a

MainActivity which extends Activity and in that class i am displaying fragments.

One of my colleague told me to extends mainActivity from Fragment . Otherwise i will have to face a lot of problem. Tell me which is the proper way to use . Or what are the benefits or disadvantage of extending mainActivity from Activity while using Fragments...

Thanks in Advance..

4

1 回答 1

1

内普斯特,这是个好问题。

两者之间的主要区别在于,在android.app.Fragment创建之前不提供对设备的支持。所以它会很好地实施FragmentActivity

但是,如果您想将手机支持回 Android 2,则需要使用android.support.app.Fragment. 如果您需要,这很容易做到,所以如果您不知道需要支持哪些设备,最好通过提供支持来覆盖您的基础。

FragmentActivity和Activity的区别在于FragmentActivity允许你使用getSupportLoaderManager()和getSupportFragmentManager(),而Activity使用getLoaderManager()和getFragmentManager()。是优点还是缺点取决于你是否需要与 FragmentActivity 相关的支持库。这是唯一的区别,FragmentActivity 允许您使用支持库。

于 2014-06-26T11:43:35.390 回答