Android: how to notify Activity when Fragments views are ready?

前端 未结 4 854
陌清茗
陌清茗 2020-12-14 07:34

I\'m using Google Maps API V2 in an activity wih drop down navigation where the map is on the second position.

I\'m adding the map pragmatically like:



        
4条回答
  •  一整个雨季
    2020-12-14 07:53

    If you want to do it without any listener:

    Add Fragment with a TAG

     supportFragmentManager
                    .beginTransaction()
                    .add(R.id.pagerContainer, UniversalWebViewFragment.newInstance(UniversalWebViewFragment.YOUTUBE_SERACH_URL+"HD trailers"), 
                    "UniversalWebView")
                    .disallowAddToBackStack()
                    .commit()
    

    Create a public method in Hosting Activity class which you want to call after fragment is loaded. Here I am calling back a method of my fragment for example

    public fun loadURL() {
            val webViewFragment = supportFragmentManager
                                  .findFragmentByTag("UniversalWebView") 
                                   as UniversalWebViewFragment
    
            webViewFragment.searchOnYoutube("Crysis Warhead")
        }
    

    Now inside onViewCreated Method of your fragment you can simply call that public method of Host activity like this:

        (activity as HomeActivity ).loadURL()
    

提交回复
热议问题