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

fix fragment not attached to context exception #363

Merged
merged 2 commits into from
Jun 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ class ImagePickerLauncher(
typealias ImagePickerCallback = (List<Image>) -> Unit

fun Fragment.registerImagePicker(
context: Context,
callback: ImagePickerCallback
): ImagePickerLauncher {
return ImagePickerLauncher(requireContext(), createLauncher(callback))
return ImagePickerLauncher(context, createLauncher(callback))
}

fun ComponentActivity.registerImagePicker(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ class CustomUIActivity : AppCompatActivity() {
}

private fun setupView() {
setSupportActionBar(binding.toolbar as Toolbar)
setSupportActionBar(binding.toolbar.root as Toolbar)
checkNotNull(supportActionBar)

actionBar = supportActionBar!!
Expand Down
21 changes: 12 additions & 9 deletions sample/src/main/java/com/esafirm/sample/MainFragment.kt
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
package com.esafirm.sample

import android.content.Context
import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import com.bumptech.glide.Glide
import com.esafirm.imagepicker.features.ImagePickerConfig
import com.esafirm.imagepicker.features.ImagePickerMode
import com.esafirm.imagepicker.features.ReturnMode
import com.esafirm.imagepicker.features.registerImagePicker
import com.esafirm.imagepicker.features.*
import com.esafirm.sample.databinding.FragmentMainBinding

class MainFragment : Fragment(R.layout.fragment_main) {

private lateinit var binding: FragmentMainBinding

private val imagePickerLauncher = registerImagePicker {
val firstImage = it.firstOrNull() ?: return@registerImagePicker
Glide.with(binding.imgFragment)
.load(firstImage.uri)
.into(binding.imgFragment)
private lateinit var imagePickerLauncher: ImagePickerLauncher

override fun onAttach(context: Context) {
super.onAttach(context)
imagePickerLauncher = registerImagePicker(context) {
val firstImage = it.firstOrNull() ?: return@registerImagePicker
Glide.with(binding.imgFragment)
.load(firstImage.uri)
.into(binding.imgFragment)
}
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
Expand Down