Get Fragment dynamically attached to ?

前端 未结 1 1894
不思量自难忘°
不思量自难忘° 2020-12-08 07:07

Well, i got a simple :



        
1条回答
  •  粉色の甜心
    2020-12-08 07:43

    Let me wrap it up by a full answer :)

    In this case, the dynamically added Fragment uses the ID of the container View (ViewGroup).

    ref: http://developer.android.com/guide/components/fragments.html#Adding

    Note: Each fragment requires a unique identifier that the system can use to restore the fragment if the activity is restarted (and which you can use to capture the fragment to perform transactions, such as remove it). There are three ways to provide an ID for a fragment:

    • Supply the android:id attribute with a unique ID.
    • Supply the android:tag attribute with a unique string.
    • If you provide neither of the previous two, the system uses the ID of the container view.

    It is because it's a Fragment afterall. We have to use getSupportFragmentManager().findFragmentById() to retrieve it, which returns a Fragment, instead of findViewById() which returns a View.

    So the answer to this problem would be:

    ((aFrag) getSupportFragmentManager().findFragmentById(R.id.FragmentContainer))
    

    Thanks to @Luksprog.

    0 讨论(0)
提交回复
热议问题