0

I have this code

class AddReminderFragment : BaseComposeFragment() {
    @Composable
    override fun Content() {
        // this is null when i add @AndroidEntryPoint to this fragment
        val activity = LocalContext.current as? ComponentActivity
    }
}

BaseComposeFragment is just a little helper i'm using and it looks like that:

abstract class BaseComposeFragment : Fragment() {

    @Composable
    abstract fun Content()

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ) = ComposeView(requireContext()).apply {
        setContent {
            MyTheme {
                this@BaseComposeFragment.Content()
            }
        }
    }
}

The issue I'm having is that I'm trying to get ComponentActivity like that val activity = LocalContext.current as? ComponentActivity and it's working fine until i add @AndroidEntryPoint to AddReminderFragment. When i do that the LocalContext.current have different value: ViewComponentManager$FragmentContextWrapper which cannot be cast to ComponentActivity giving me null instead of activity.

Why is that happening and how can i prevent that?

Jakoss
  • 4,647
  • 2
  • 26
  • 40
  • Does this answer your question? [How to get activity in compose](https://stackoverflow.com/questions/64675386/how-to-get-activity-in-compose) – Phil Dukhov Oct 05 '21 at 13:46
  • 1
    Well this gives me a way to bypass that, but since the issue i'm having is due to library that asserts that it have to be `CompatActivity` then this won't really solve my issue. And also i'd like to know why adding entry point annotation changes value here – Jakoss Oct 05 '21 at 13:49
  • 1
    check out [this answer](https://stackoverflow.com/a/64342511/3585796) – Phil Dukhov Oct 05 '21 at 19:02

1 Answers1

0
= ComposeView(requireContext()).apply

Use requireActivity() here instead of requireContext()