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.



