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

前端 未结 4 1272
走了就别回头了
走了就别回头了 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: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.

提交回复
热议问题