Fragment 小结

Fragment 小结

总结下昨天的学习:

1,Fragment 可以看作Activity的组件,因为一种Fragment可以在任意个Activity中被调用任意次,其有自己的lifes cycle,但受Activity影响,Fragment的onCreateView方法处理自己的layout。

2,将Fragment添加到Activity中有两种方法:

在Activity的mian  layout中用标签<fragment name=“。。。”>,其外一种<Fragment ...>在代码中FragmentTransaction添加。

3,Fragment和Activity的区别

One of the key differences between a Fragment and an Activity is that Fragments instantiate their Views inside the onCreateView() callback and Activities instantiate their Views using the setContentView() method inside of the onCreate() callback. Fragments also have to manually instantiate their Views using an instance of LayoutInflater, which is provided to the onCreateView() method for convenience.


Another difference is that a Fragment is not a subclass of Context. This means that a Fragment can not be launched as a component inside your app and therefore always has to live inside of an Activity. This also means that whenever you need a Context inside of a Fragment, you need to get access to the parent Activity. You can do this by using the getActivity() method as we have done in the Fragment button's OnClickListener callback. You need to watch out because getActivity() can return null depending on where the Fragment is in the Activity's lifecycle. So, you should also include a check to see if the Activity is null before you use it.

4,Fragment在Android3.0及更高版本的系统上才能使用,但在引入android-support-v4.jar这个包后,在2.1的系统也能使用了。

你可能感兴趣的:(layout,null,Access,callback,include,button)