Fragment uses findFragmentById to return null, findfragmentbyid
After the new version of ADT is updated today, a project is established by default to introduce Fragment, which has not been known before.
The Code is as follows:
protected void onCreate(Bundle savedInstanceState) { Log.d(TAG, "" + "ActionBarActivity onCreate"); super.onCreate(savedInstanceState); setContentView(R.layout.activity_jnitest); if (savedInstanceState == null) { getSupportFragmentManager().beginTransaction() .add(R.id.container, new PlaceholderFragment(), "myfragment") .commit(); } Log.d(TAG, "Fragment:"+getSupportFragmentManager().findFragmentById(R.id.container));
The log is printed as null. After reading the Fragment lifecycle, you may not be able to obtain the Fragment after it is created. Try another place. In onResume of Activity, it must be created here.
protected void onResume() { Log.d(TAG, "" + "ActionBarActivity onResume"); Log.d(TAG, "Fragment:"+getSupportFragmentManager().<span style="font-family: Arial, Helvetica, sans-serif;">findFragmentById(R.id.container));</span> // TODO Auto-generated method stub super.onResume(); }The result is still null. See what Google Developer can obtain
Public abstract Fragment findFragmentById (int id) Added in API level 11
Finds a fragment that was identified by the given id either when inflated from XML or as the container ID when added in a transaction. this first searches through fragments that are currently added to the manager's activity; if no such fragment is found, then all fragments currently on the back stack associated with this ID are searched.
Returns
- The fragment if found or null otherwise.
My E files are poor. In my understanding, as long as transaction is used, the given container ID can be obtained, but it is always null.
Someone asked a similar question in google stackoverflow, but almost everyone replied that this was not the case. findFragmentById can only be used in the ID marked in XML.
That is to say, you can search by using the code similar to the following through R. id. list:
<fragment android:name="com.example.news.ArticleListFragment" android:id="@+id/list" android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent" />
That is to say, the rest can only be searched through findFragmentByTag, and a myfragment tag is added during creation.
if (savedInstanceState == null) { getSupportFragmentManager().beginTransaction() .add(R.id.container, new PlaceholderFragment(), "myfragment") .commit(); }Use the following code to find
Log.d(TAG, "Fragment:"+getSupportFragmentManager().findFragmentByTag("myfragment"));
The second problem I encountered was that I didn't use getsuppfrfragmentmanager, but the getFragmentManager results were still blank.
I don't understand. What is the difference between getSupportFragmentManager and getFragmentManager.
After google, the functions of the two methods are the same. The difference lies only in the version difference. If the minSdk you configured in AndroidManifest is less than 14, you should use getsuppfrfragmentmanager.
If it is higher than 14 (inclusive), use getFragmentManager.
The reason is that the jar package is different, I use a android-support-v4.jar.
If Fragment is overwritten, the returned result is null. Why?
I solved the problem. You don't need any help!
How to refresh data when fragment presses the back key
You can use onactivityresult to override this method in the Activity, and then the Activity can get two Fragment. For example, if A fragment has A public void refresh () method, you can call it. I have never tried it. Try it. The other method is to directly obtain the parent Activity. For example, you are using AparentActivity parent = (AparentActivity) getActivity (); parent. getAFragment (). refresh (); in this way, this statement can be called at the end of BFragment. onDestroy