cihatid
cihatid

Reputation: 19

Change Fragment in Navigation Drawer Activity (Kotlin)

I've just started using Fragments and ViewModels. I want to replace fragments inside my activity, how can I do that?

What is the best approach to mix Android and Fragments together?

MainActivity

class MainActivity : AppCompatActivity(),NavigationView.OnNavigationItemSelectedListener {

    lateinit var drawerLayout: DrawerLayout
    lateinit var fragmentTransaction: FragmentTransaction

    private lateinit var appBarConfiguration: AppBarConfiguration


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val toolbar: Toolbar = findViewById(R.id.toolbar)
        setSupportActionBar(toolbar)

        drawerLayout= findViewById(R.id.drawer_layout)
        val navView: NavigationView = findViewById(R.id.nav_view)
        val navController = findNavController(R.id.nav_host_fragment)

        val toggle = ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.navigation_drawer_open,R.string.navigation_drawer_open)
            drawerLayout.addDrawerListener(toggle)
            toggle.syncState()


        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        appBarConfiguration = AppBarConfiguration(setOf(
                R.id.nav_home, R.id.nav_dersler, R.id.nav_soru,R.id.nav_socialmedia), drawerLayout)
        setupActionBarWithNavController(navController, appBarConfiguration)
        navView.setupWithNavController(navController)

        navView.setNavigationItemSelectedListener(this);

    }

    override fun onNavigationItemSelected(item: MenuItem): Boolean {
        when(item.itemId){
            R.id.nav_home -> {
                  //Toast.makeText(this,"Anasayfa",Toast.LENGTH_SHORT).show()
                }
                R.id.nav_socialmedia -> {
                    Toast.makeText(this,"Sosyal Medya Hesaplarımızı Takip Edebilirsiniz",Toast.LENGTH_SHORT).show()
                }

                R.id.nav_dersler -> {


                }
                R.id.nav_soru -> {

                }


            }
            drawerLayout.closeDrawer(GravityCompat.START)
            return true

        }


        override fun onCreateOptionsMenu(menu: Menu): Boolean {
            menuInflater.inflate(R.menu.main, menu)
            return true
        }




        override fun onOptionsItemSelected(item: MenuItem): Boolean {
             when(item.itemId){
                R.id.action_refresh -> {
                    Toast.makeText(this,"Refreshing...",Toast.LENGTH_SHORT).show()
                    val refreshActivity = intent
                    finish()
                    startActivity(refreshActivity)
                    true
                }

                else -> super.onOptionsItemSelected(item)
            }
            return true
        }

        override fun onSupportNavigateUp(): Boolean {
            val navController = findNavController(R.id.nav_host_fragment)
            return navController.navigateUp(appBarConfiguration) || super.onSupportNavigateUp()

        }


}

And i have mobile_navigation.xml but i don't know what it does. Thank you for your help enter image description here

Upvotes: 0

Views: 3253

Answers (1)

Arrowsome
Arrowsome

Reputation: 2859

TL;DR:

Activity is your base component for making Screens in your app. Fragment is a dynamic part of your screen which was added to support Tablets with bigger screens.

We usually combine them in these manners:

  1. One Activity as a container and all the screens represented as Fragments

For this approach, you can either

Implement it on your own:

Add fragments to your Activity stack:

supportFragmentManager
        .beginTransaction()
        .addToBackStack(null)
        .add(containerId, fragment, fragment::class.java.simpleName)
        .commit()

Replace fragments in your Activity:

supportFragmentManager
        .beginTransaction()
        .replace(containerId, fragment, fragment::class.java.simpleName)
        .commit()

But Android introduces Android Navigation Component to handle all these add, replacement and back stack handling:

I suggest using this approach, as its the easier one. LINK

  1. Use multiple activities and fragments

For each screen that uses BottomNavigation, NavigationDrawer, and ... use an Activity with multiple Fragments representing each tab inside them.

A good tutorial can be found here. LINK

Upvotes: 1

Related Questions