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. TCP系列38—拥塞控制—1、概述

    在本篇中我们继续上一篇文章wireshark的示例讲解,上一篇介绍了一个综合示例后,本篇介绍一些简单的示例,在读本篇前建议先把上一篇读完,为了节省篇幅,本篇只针对一些特殊的场景点报文进行讲解,不会像上 ...

  2. C# Find()和First()与FirstOrDefault(

    1. Find方法只能在List<T>上使用,而后者能更广泛应用在IEnemerable<T>上. Find最终是建立在Array的查找之上,而在IEnemerable上的Fi ...

  3. C# 7.0 新特性

    先列一下相关的语法: 1.out-variables(Out变量) 2.Tuples(元组) 3.Pattern Matching(匹配模式) 4.ref locals and returns (局部 ...

  4. perf中branch-filter到底是干嘛的?

    ./arch/x86/events/intel/core.c:2161:            data.br_stack = &cpuc->lbr_stack;./arch/x86/e ...

  5. Delphi DBGrid双击事件、单元格操作

    1.得到当前格子中的内容:DBGrid1.Fields[DBGrid1.SelectedIndex].DisplayText;把DBGrid1.SelectedIndex改为你所希望引用的字段就可以了 ...

  6. SpringBoot事件监听

    代码演示: package com.boot.event.eventdemo; import org.springframework.boot.SpringApplication; import or ...

  7. 单选 name的值相同时候 就会产生互斥现象

  8. SPFA判負環

    馬上就退役了,時間不足就不多介紹了 反正DFS是會T飛的,BFS就沒關係了qwq #include<cmath> #include<queue> #include<cst ...

  9. [luogu3806]【模板】点分治1

    description 求树上长度为\(k\)的路径是否存在. data range \[n\le 10000,k\le 10000000\] solution 点分治复习... 使用普通的点分治枚举 ...

  10. [bzoj] 2049 洞穴勘探 || LCT

    原题 这是一道LCT的板子题. 至于LCT--link cut tree,也叫动态树,用splay实现动态连边的树. 预备知识: 实边:一个非叶节点,向它的儿子中的一个连一条特殊的边,称为实边;该非叶 ...