标签:
fragment的用法:
FragmentManager fm = getSupportFragmentManager(); //fragment在队列存在,fragmentManager会返还这个fragment,不存在则返回null Fragment fragment = fm.findFragmentById(R.id.fragment_container); if (fragment == null){ fragment = new CrimeFragment(); fm.beginTransaction().add(R.id.fragment_container,fragment).commit(); }
1.findFragmentById的理解
Fragment fragment = fm.findFragmentById(R.id.fragment_container);
之前一直不理解为什么要加这行代码,看了后面的解释才发现fm.findFragmentById(R.id.fragment_container)的作用是在fragment在队列存在,fragmentManager会返还这个fragment,
不存在则返回null,而R.id.fragment_container是fragment在FragmentManager队列中的唯一标识符。
2.SDK版本
如果SDK的最低版本低于API11,用支持库来使用fragment;如果SDK的最低版本是11或者更高,可以直接使用标准库中的原生fragment类。
To use standard library fragments, you would make three changes to the project:
Subclass the standard library Activity class (android.app.Activity) instead of FragmentActivity. Activities have support for fragments out of the box on API level 11 or higher.
Subclass android.app.Fragment instead of android.support.v4.app.Fragment.
To get the FragmentManager, call getFragmentManager() instead of
getSupportFragmentManager().
标签:
原文地址:http://www.cnblogs.com/luoqiuyu/p/5943991.html