Skip to content

Instantly share code, notes, and snippets.

@ekoo
Last active October 12, 2022 00:45
Show Gist options
  • Save ekoo/21250bc708038a62cc9cdde4353673af to your computer and use it in GitHub Desktop.
Save ekoo/21250bc708038a62cc9cdde4353673af to your computer and use it in GitHub Desktop.
Base class for single activity using multiple fragment with compose view
import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.compose.runtime.Composable
import androidx.compose.ui.platform.ComposeView
import androidx.fragment.app.Fragment
import com.google.android.material.transition.MaterialSharedAxis
abstract class ComposeFragment: Fragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enterTransition = MaterialSharedAxis(MaterialSharedAxis.Z, true)
exitTransition = MaterialSharedAxis(MaterialSharedAxis.Z, true)
reenterTransition = MaterialSharedAxis(MaterialSharedAxis.Z,false)
returnTransition = MaterialSharedAxis(MaterialSharedAxis.Z, false)
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
) = ComposeView(inflater.context).apply {
isTransitionGroup = true
layoutParams = ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT
)
setContent{
ComposableContent()
}
}
@Composable
abstract fun ComposableContent()
}
/** Sample usage
*
* class HomeFragment: ComposeFragment() {
*
* @Composable
* override fun ComposableContent() {
* Text(text = "Hello Compose")
* }
* }
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment