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. c# 生成的没用文件

    1.pdb 2.vhost 3.application 4.含有更新功能(更新文件夹)

  2. BluetoothAdapter解析

    这篇文章将会详细解析BluetoothAdapter的详细api, 包括隐藏方法, 每个常量含义. 一 BluetoothAdapter简介 1.继承关系 该类仅继承了Object类; 2.该类作用 ...

  3. jQuery对象和DOM对象使用说明

    1.jQuery对象和DOM对象第一次学习jQuery,经常分辨不清哪些是jQuery对象,哪些是 DOM对象,因此需要重点了解jQuery对象和DOM对象以及它们之间的关系.DOM对象,即是我们用传 ...

  4. caffe2安装教程

    相比于网上的安装教程不如直接看官方安装教程:https://caffe2.ai/docs/getting-started.html?platform=windows&configuration ...

  5. 个人作业4 alpha阶段 个人总结

    一.个人总结 二.回答问题 三.再提问题 Q1:关于第三章过早优化 过早优化:既然软件是"软"的,那么它就有很大的可塑性,可以不断改进.放眼望去,一个复杂的软件似乎很多的模块都可以 ...

  6. ubuntu下修改MySQL的配置文件my.cnf

    先sudo su转换成root,再用cd转到/etc/MySQL目录下,用chmod修改权限(chmod 755 my.cnf),但这样还不能修改,再用vi命令(vi my.cnf),通过上下方向键将 ...

  7. 【alpha】Scrum站立会议第1次····10.16

    小组名称:nice! 小组成员:李权 于淼 杨柳 刘芳芳 项目内容:约跑app 1.任务进度 成员 已完成 当日要完成 李权 搭建好Android Studio环境 数据库设计 于淼 搭建好Andro ...

  8. 关于如何解决PHPCMS V9内容搜索显示不全问题解决方案

    站长朋友们都晓得只要是开源的PHP程序都会有漏洞存在.如果想完美的建站就需要自己去研究打补丁了.最近很多站长联系小编咨询用phpcms建站当在首页搜索内容的时候有的居然搜索不到.小编感到很是奇怪于是就 ...

  9. Agile.Net 组件式开发平台 - 驱动开发示例

    首先讲一下概念,此驱动非彼驱动.在Agle.Net中我们将组件规划成两种类型,一种是基于业务的窗体组件,一种是提供扩展功能的驱动组件. 打个比方例如一般系统中需要提供身份证读卡功能,然而市面上有很多种 ...

  10. google go语言开发

    C:cd C:\Program Files\go\gopath\src\opmsset GOOS=linuxset GOARCH=amd64set CGO_ENABLED=0make.batgo in ...