Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save arohim/32e4c899396e349413fb87c1714a5b61 to your computer and use it in GitHub Desktop.
Save arohim/32e4c899396e349413fb87c1714a5b61 to your computer and use it in GitHub Desktop.
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.fragment.NavHostFragment
import com.xxx.R
import kotlinx.android.synthetic.main.activity_nav_com.*
abstract class NavComActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_nav_com)
setUpGraph()
}
private fun setUpGraph() {
val navHostFragment = navigation_host_fragment as NavHostFragment
val inflater = navHostFragment.navController.navInflater
val graph = inflater.inflate(navResId)
navHostFragment.navController.setGraph(graph, intent.extras)
}
abstract var navResId: Int
}
// activity_nav_com.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<fragment
android:id="@+id/navigation_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true" />
</LinearLayout>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment