public View onCreateView(LayoutInflater inflater, ViewGroup contaiiner, Bundle savedInstanceState)

在写一个Fragment的时候,继承Fragment基类,然后,要重写的其中一个回调方法是onCreateView。如果该Fragment有界面,那么,返回的View是非空的;如果该Fragment

是没有界面的,返回的是Null。

这是在写Fragment中经常做的事情。不过,这里有个小细节,那就是什么时候container是为空的,为空表示什么?

这就是本篇文章要解决的问题。

写一个Demon之后,观察,发现了如下事实:

1.首先是要Fragment在activity的UI中出现了,也就是说,一开始container是不可能为Null的。

2.当因为其他情况,导致了Fragment所依附的父组件不存在了,那么此时container就是Null了。----比如,从横屏转换到竖屏,就会导致之前的界面发生改变。

所以,答案为:

当Fragment所依附的container,从有到无,就会导致container为空。空表示,当前Fragment所依附的ViewGroup不存在了(从有到没,一开始是有的)。

实现测试的Demon:

1.两个界面,一个是系统横屏时使用的,一个是系统竖屏时使用的。横屏时,会生成Fragment。

先横屏,在界面中产生了Fragment;然后,再竖屏,此时系统使用另外一个布局文件,之前Fragment所依附的ViewGroup消失了,这时系统调用onCreateView,container为空。

写一个监听器,将Fragment所接受到的信息传递给宿主Activity。

判定当前,系统使用哪个布局文件(相应的表示了当前是横屏还是竖屏),是横屏,则实例化Fragment,并添加到相应的ViewGroup中。

package com.containertest;

import com.containertest.fragment.SimpleFragment;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Toast; public class MainActivity extends Activity implements ContainerDetectListener { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); if (findViewById(R.id.fragment) != null) {
// 将Fragment添加到R.id.fragment所指向的布局中,R.id.fragment所指向的布局是container
SimpleFragment f = SimpleFragment.newInstanec();
getFragmentManager().beginTransaction().add(R.id.fragment, f)
.commit(); } else {
// 此时是竖着拿手机,不用做任何操作 } } @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} @Override
public void containerIsNull(boolean state) {
// TODO Auto-generated method stub
if (state) {
Toast.makeText(this, "Now the onCreateView's container is null",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this,
"Now the onCreateView/s container is not null",
Toast.LENGTH_SHORT).show();
} } }

SimpleFragment:

package com.containertest.fragment;

import com.containertest.ContainerDetectListener;

import com.containertest.R;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; public class SimpleFragment extends Fragment { private ContainerDetectListener listener; private static SimpleFragment f; public static SimpleFragment newInstanec() {
if (f == null) {
f = new SimpleFragment();
} return f; } @Override
public void onAttach(Activity activity) {
super.onAttach(activity); try {
listener = (ContainerDetectListener) activity; } catch (ClassCastException e) {
e.printStackTrace();
throw new ClassCastException(activity.toString()
+ "must implement ContainerDetectListener");
}
} @Override
public void onCreate(Bundle state) {
super.onCreate(state); } @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle state) { if (container == null) {
listener.containerIsNull(true);
return null;
} else {
// 计划通过代码来制定container
listener.containerIsNull(false);
View view = inflater.inflate(R.layout.fragment_ui, null);
return view;
} } @Override
public void onPause() {
super.onPause();
} }

效果图:

开始,本身就是没有生成过Fragment:是竖屏。

然后,将手机横屏:---将会生成fragment

---因为会重新创建Activity,从而再次检测当前界面是横屏还是竖屏。

然后,再将手机竖屏了----这时,Fragment依然是那个Fragment,不过,因为,它所依附的ViewGroup不存在了,所以,无需给它绘制界面。

----这是container为Null的情况才会出现。

附上:官方关于该方法的解释

public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)

Added in API level 11

Called to have the fragment instantiate its user interface view. This is optional, and non-graphical fragments can return null (which is the default implementation). This will be called between onCreate(Bundle) and onActivityCreated(Bundle).

If you return a View from here, you will later be called in onDestroyView() when the view is being released.

Parameters

inflater
The LayoutInflater object that can be used to inflate any views in the fragment,

container
If non-null, this is the parent view that the fragment's UI should be attached to. The fragment should not add the view itself, but this can be used to generate the LayoutParams of the view.

savedInstanceState
If non-null, this fragment is being re-constructed from a previous saved state as given here.

Returns
  • Return the View for the fragment's UI, or null.

onCreateView的一个细节--Fragment的更多相关文章

  1. Luogu3163 [CQOI2014]危桥 ---- 网络流 及 一个细节的解释

    Luogu3163 [CQOI2014]危桥 题意 有$n$个点和$m$条边,有些边可以无限次数的走,有些边这辈子只能走两次,给定两个起点和终点$a_1 --> a_2$(起点 --> 终 ...

  2. Eclipse创建第一个Servlet(Dynamic Web Project方式)、第一个Web Fragment Project(web容器向jar中寻找class文件)

    创建第一个Servlet(Dynamic Web Project方式) 注意:无论是以注解的方式还是xml的方式配置一个servlet,servlet的url-pattern一定要以一个"/ ...

  3. Protoc Buffer 优化传输大小的一个细节

    Protoc Buffer 是我们比较常用的序列化框架,Protocol Buffer 序列化后的占空间小,传输高效,可以在不同编程语言以及平台之间传输.今天这篇文章主要介绍 Protocol Buf ...

  4. 【JOB】Oracle中JOB的创建方法以及一个细节的探究

    在Oracle中可以使用JOB来实现一些任务的自动化执行,类似于UNIX操作系统crontab命令的功能.简单演示一下,供参考. 1.创建表T,包含一个X字段,定义为日期类型,方便后面的定时任务测试. ...

  5. 读《锋利的jQuery》中first-child时的一个细节

    今天在看<锋利的jQuery>这书时,看到过滤选择器那一节.有个知识点引起了我的注意. (我不用书里一模一样的代码做例子)举个简单的例子-代码: <ul> <li> ...

  6. java Integer包装类装箱的一个细节

    原文:https://www.cnblogs.com/JackPn/p/9392145.html java有八个基本数据类型,每个都有对应的一个包装类,比如int对应的Integer.从jdk1.5开 ...

  7. Swift 函数做参数和闭包做参数的一个细节差别

    函数作参数,示例为传入一个String和一个添加前缀的函数,返回一个添加完前缀的String: func demo(str:String,addPrefix:(String)->String)- ...

  8. 关于移动端click事件绑定的一个细节

    click是最常见的点击事件,但是对于移动终端,比如手机,一般都是以touch事件代替的,而click事件在手机也是生效的,只是会有1-2秒左右的延迟,那么当你想要用click而非touch事件的时候 ...

  9. 新手在sae部署程序容易忽略的一个细节

    从来没用过这类云空间服务,尝了下鲜试用一下sae,但是部署的时候发现问题了,各种404..各种无奈啊..虽然百度无数篇介绍,但是都千篇一律没什么启发.. 但是巧在我部署的应用有个欢迎页面,点击链接的时 ...

随机推荐

  1. servlet映射路径

    1 访问映射过程 问题:访问URL:http://localhost:8080/day10/first  ,服务器如何相应的? 前提: tomcat服务器启动时,首先加载webapps中的每个web应 ...

  2. ACM 第二天

    A - Mishka and Contest Mishka started participating in a programming contest. There are n problems i ...

  3. C# Dsoframer.ocx 如何在winform中嵌入Excel,内嵌Excel,word

    如果你还不太清楚Dspframer.ocx怎么放到窗体上就看上一篇文章,里面详细介绍了是如何放到窗体上的. 链接:http://www.cnblogs.com/pingming/p/4182045.h ...

  4. 3dContactPointAnnotationTool开发日志(十)

      要是那几个状态栏不能拖动的话岂不是显得太呆板了,于是我又参考Unity官方视频教程学习了如何实现拖动状态栏的功能,还挺简单的.   比如说要拖动这个PanelStatus面板,我只让使用者通过拖动 ...

  5. tomcat执行shutdown.sh进程残留的解决办法

    我们执行shutdown.sh指令的时候有时会发现进程并没有被关掉而是越来越多,这种情况一般是项目造成的,具体原因未去调查.由于tomcat自己有相应的保护机制,所以我们只需要强制结束其进程即可,下面 ...

  6. 【bzoj1738】[Usaco2005 mar]Ombrophobic Bovines 发抖的牛 Floyd+二分+网络流最大流

    题目描述 FJ's cows really hate getting wet so much that the mere thought of getting caught in the rain m ...

  7. 【bzoj1737】[Usaco2005 jan]Naptime 午睡时间 dp

    题目描述 Goneril is a very sleep-deprived cow. Her day is partitioned into N (3 <= N <= 3,830) equ ...

  8. sqlserver修改sa密码(在windows登陆没有权限的情况下)

    对于windows用户没有权限执行alter login sa enable的情况下,采用如下方法可以成功修改sa密码登陆. . 用Run as a administrator打开命令提示符里输入NE ...

  9. Android 4.0源码结构

    Android 4.0 |-- Makefile |-- bionic (bionic C库) |-- bootable (启动引导相关代码) |-- build (存放系统编译规则及generic等 ...

  10. [洛谷P1552][APIO2012]派遣

    题目大意:有一棵$n$个点的树,和一个费用$m$,每个点有一个费用和价值,请选一个点,再从它的子树中选取若干个点,使得那个点的价值乘上选的点的个数最大,要求选的点费用总和小于等于$m$ 题解:树形$d ...