Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Map Fragment is null if setContentView is not called #15

Closed
kuwapa opened this issue Nov 5, 2020 · 4 comments
Closed

Support Map Fragment is null if setContentView is not called #15

kuwapa opened this issue Nov 5, 2020 · 4 comments

Comments

@kuwapa
Copy link

kuwapa commented Nov 5, 2020

Thanks for this library. I just integrated it into an activity with a map.

private val binding: GoogleMapSettingActivityBinding by viewBinding(GoogleMapSettingActivityBinding::inflate)

and then in onCreate, I did not called setContentView as was mentioned in the documentation.

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val mapFragment: SupportMapFragment? =
                supportFragmentManager.findFragmentById(R.id.map) as? SupportMapFragment
        mapFragment?.getMapAsync(this)
        ...

The problem here is that mapFragment is null. However, if I call setContentView(binding.root) before calling findFragmentById then mapFragment is not null, like this -

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(v.root)
        ...

What could be the issue?

@kuwapa
Copy link
Author

kuwapa commented Nov 5, 2020

On looking further into this, I released this is happening because the createBinding() {} which is annotated with @OnLifecycleEvent(Lifecycle.Event.ON_CREATE) in ActivityViewBindingDelegate is called after onCreate() in the activity is done executing.

So everything in onCreate() which expects the views to be already on the screen and rendered are not present at all. Seems like this is how lifecycle observer is supposed to work. So this way of view-binding wont work for me. setContentView needs to be called after super.onCreate and not somewhere after or in the middle of onCreate() execution.

Is there someway this can be done?

@FunkyMuse
Copy link
Owner

There are few options you can use here.

  1. Move your logic in onPostCreate (i think you didn't read the updated article i made an issue the first time i published it, instead of onCreate i was supposed to use onPostCreate)
  2. use by viewBinder() and manually set the setContentView like you do

@FunkyMuse
Copy link
Owner

Take a look in the updated README

@kuwapa
Copy link
Author

kuwapa commented Nov 5, 2020

Option 1 is good. I'd forgotten about onPostCreate. But I'm gonna go with option 2 for now as it'll need minimum changes. Thanks.

@kuwapa kuwapa closed this as completed Nov 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants