zoukankan      html  css  js  c++  java
  • FragmentActivity和Activity的具体区别在哪里

    fragment是3.0以后的东西,为了在低版本中使用fragment就要用到android-support-v4.jar兼容包,而fragmentActivity就是这个兼容包里面的,它提供了操作fragment的一些方法,其功能跟3.0及以后的版本的Activity的功能一样。
    下面是API中的原话:
    FragmentActivity is a special activity provided in the Support Library to handle fragments on system versions older than API level 11. If the lowest system version you support is API level 11 or higher, then you can use a regular Activity.

    1、fragmentactivity 继承自activity,用来解决android3.0 之前没有fragment的api,所以在使用的时候需要导入support包,同时继承fragmentActivity,这样在activity中就能嵌入fragment来实现你想要的布局效果。 
    2、当然3.0之后你就可以直接继承自Activity,并且在其中嵌入使用fragment了。 
    3、获得Manager的方式也不同 
    3.0以下:getSupportFragmentManager() 
    3.0以上:getFragmentManager() 

    Fragment is a section of an Activity, which has:

    • its own lifecycle
    • receives its own input events
    • can be added or removed while the Activity is running.

    Fragment must always be embedded in an Activity.

    Fragments are not part of the API prior to HoneyComb (3.0). If you want to use Fragments in an app targeting a platform version prior to HoneyComb, you need to add the Support Package to your project and use the FragmentActivity to hold your Fragments. The FragmentActivity class has an API for dealing with Fragments, whereas the Activity class, prior to HoneyComb, doesn't.

    If your project is targeting HoneyComb or newer only, you should use Activity and notFragmentActivity to hold your Fragments.

    Some details:

    Use android.app.Fragment with Activity. Use android.support.v4.app.Fragment withFragmentActivity. Don't add the support package Fragment to an Activity as it will cause an Exception to be thrown.

    A thing to be careful with: FragmentManager and LoaderManager have separate support versions for FragmentActivity:

    If you are using a Fragment in an Activity (HoneyComb and up), call

    • getFragmentManager() to get android.app.FragmentManager
    • getLoaderManager() to get android.app.LoaderManager

    if you are using a Fragment in a FragmentActivity (pre-HoneyComb), call:

    • getSupportFragmentManager() to get android.support.v4.app.FragmentManager.
    • getSupportLoaderManager() to get android.support.v4.app.LoaderManager

    so, dont do

    myFragmentActivity.getLoaderManager()//don't do this, do myFragmentActivity.getSupportLoaderManager()

    or

    android.app.FragmentManager fm = myFragmentActivity.getSupportFragmentManager()//don't do this, do android.support.v4.app.FragmentManager fm = myFragmentActivity.getSupportFragmentManager()

    Also useful to know is that while a fragment has to be embedded in an Activity it doesn't have to be part of the Activity layout. It can be used as an invisible worker for the activity, with no UI of its own.

    http://www.cnblogs.com/wanqieddy/p/3818718.html
  • 相关阅读:
    bzoj1600 [Usaco2008 Oct]建造栅栏(递推)
    bzoj1607 / P2926 [USACO08DEC]拍头Patting Heads
    bzoj2733 / P3224 [HNOI2012]永无乡(并查集+线段树合并)
    loj2163 / bzoj2212 / P3521 [POI2011]ROT-Tree Rotations(线段树合并)
    UVA11090 Going in Cycle!!(二分判负环)
    【C++】位应用(2)-设置某位的值
    【C++】位应用(2)-设置某位的值
    【C++】位操作的应用
    【C++】位操作的应用
    C++ 强转注意问题
  • 原文地址:https://www.cnblogs.com/jiduoduo/p/4790202.html
Copyright © 2011-2022 走看看