android.support.v4.app.FragmentManager OR android.app.FragmentManager?

前端 未结 4 1268
走了就别回头了
走了就别回头了 2021-02-08 23:20

I\'m learning Android development. I get stuck at something that should be very easy.

Im creating an App with one Activity, 2 fragments and 1 interface.

         


        
相关标签:
4条回答
  • 2021-02-08 23:34

    Simply use getSupportFragmentManager(); , after you added the support library successfully.

    0 讨论(0)
  • 2021-02-08 23:36

    First of all: your activity should extend FragmentActivity.

    About support libraries. They were introduced to add some functionalities to older Androids. For example Fragments were introduced in Android 3.0 (SDK nr: 11). In fact (according to documentation) in Androids 3.0 < support libary uses system implementation of Fragments.

    0 讨论(0)
  • 2021-02-08 23:45

    OP was so close to having a solution that would have worked fine for API 11 and newer without needing support.v4.

    He just needed to change his Fragment to also not use support.v4, in its import statement.

    Summary of the two approaches. ALL your Activities and Fragments should have code that looks like exactly one of these; do not mix them! (Not all lines are needed in all files; use lines as needed.)

    support-v4 approach

    import android.support.v4.app.FragmentActivity;
    import android.support.v4.app.Fragment;      <-- ".support.v4"
    import android.support.v4.app.FragmentManager;
    
    ... MainActivity extends FragmentActivity ...
    
    ... = getSupportFragmentManager();
    
    .... YourFragment extends Fragment ... <-- same in both approaches
    

    API 11+ approach

    import android.app.Activity;
    import android.app.Fragment;
    import android.app.FragmentManager;
    
    ... MainActivity extends Activity ...
    
    ... = getFragmentManager();
    
    .... YourFragment extends Fragment ... <-- same in both approaches
    

    So if you have a project that is written using one approach above, and you are integrating in code from elsewhere, be sure to look for these lines, and change them to match what you have.

    0 讨论(0)
  • 2021-02-08 23:59

    It's simple.

    If you also want your app to run in older devices (below API level 11), use getSupportFragmentManager().

    If you want your app to run in devices with API Level above 11, then use getFragmentManger().

    0 讨论(0)
提交回复
热议问题