Skip to content

Activity

Douglas Soares Pereira edited this page Sep 4, 2018 · 4 revisions

To start, your activity need to extend BaseStackActivity and fill the two required params:

  1. The resource id from the container (Frame Layout) where the fragments gonna be inflated;
  2. The start position of the Bottom Navigation View;
class MainActivity : BaseStackActivity(R.id.contentLayout, 2)

After that you need to implement bottomNavigationView and send the Bottom Navigation View:

override fun bottomNavigationView(): BottomNavigationView = bottomNavigation

And implement getRootFragment, that's where you gonna put your Fragment Instance and gonna be called by the position from the Bottom Navigation:

override fun getRootFragment(position: Int): Fragment {
    return when (position) {
        0 -> FirstFragment()
        1 -> SecondFragment()
        2 -> ThirdFragment()
        3 -> FourthFragment()
        4 -> FifthFragment()
        else -> ThirdFragment()
    }
}

When this is done, you need do implement BottomNavigationView.OnNavigationItemSelectedListener just like you always do but call updateStack there sending the position:

class MainActivity : BaseStackActivity(R.id.contentLayout, 2), BottomNavigationView.OnNavigationItemSelectedListener

And:

override fun onNavigationItemSelected(item: MenuItem): Boolean {
    return when (item.itemId) {
        R.id.navigation_first -> {
            updateStack(0)
            true
        }
        R.id.navigation_second -> {
            updateStack(1)
            true
        }
        R.id.navigation_third -> {
            updateStack(2)
            true
        }
        R.id.navigation_fourth -> {
            updateStack(3)
            true
        }
        R.id.navigation_fifth -> {
            updateStack(4)
            true
        }
        else -> false
    }
}

So, your activity gonna looks like this:

class MainActivity : BaseStackActivity(R.id.contentLayout, 2),
        BottomNavigationView.OnNavigationItemSelectedListener {

    override fun bottomNavigationView(): BottomNavigationView = bottomNavigation

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

        bottomNavigation.setOnNavigationItemSelectedListener(this)
    }

    override fun onNavigationItemSelected(item: MenuItem): Boolean {
        return when (item.itemId) {
            R.id.navigation_first -> {
                updateStack(0)
                true
            }
            R.id.navigation_second -> {
                updateStack(1)
                true
            }
            R.id.navigation_third -> {
                updateStack(2)
                true
            }
            R.id.navigation_fourth -> {
                updateStack(3)
                true
            }
            R.id.navigation_fifth -> {
                updateStack(4)
                true
            }
            else -> false
        }
    }

    override fun getRootFragment(position: Int): Fragment {
        return when (position) {
            0 -> FirstFragment()
            1 -> SecondFragment()
            2 -> ThirdFragment()
            3 -> FourthFragment()
            4 -> FifthFragment()
            else -> ThirdFragment()
        }
    }
}

Extra

If your app needs to know the end of stack, for example an app that shows a message before finish, you can do that implementing FragmentStackManager.OnBackPressed and your activity gonna looks like this:

class MainActivity : BaseStackActivity(R.id.contentLayout, 2),
        BottomNavigationView.OnNavigationItemSelectedListener,
        FragmentStackManager.OnBackPressed {

    override fun bottomNavigationView(): BottomNavigationView = bottomNavigation

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

        bottomNavigation.setOnNavigationItemSelectedListener(this)
        setOnStackBackPressedListener(this)
    }

    override fun onNavigationItemSelected(item: MenuItem): Boolean {
        return when (item.itemId) {
            R.id.navigation_first -> {
                updateStack(0)
                true
            }
            R.id.navigation_second -> {
                updateStack(1)
                true
            }
            R.id.navigation_third -> {
                updateStack(2)
                true
            }
            R.id.navigation_fourth -> {
                updateStack(3)
                true
            }
            R.id.navigation_fifth -> {
                updateStack(4)
                true
            }
            else -> false
        }
    }

    override fun getRootFragment(position: Int): Fragment {
        return when (position) {
            0 -> FirstFragment()
            1 -> SecondFragment()
            2 -> ThirdFragment()
            3 -> FourthFragment()
            4 -> FifthFragment()
            else -> ThirdFragment()
        }
    }

    override fun onStackEnded() {
        //Do something awesome but never forget the finish()
    }
}

Clone this wiki locally