Search code examples
javaandroidkotlinbottomnavigationview

What is wrong with my OnNavigationItemSelectedListener kotlin android?


I'm trying to use bottomNavigation at my application. Right now I would like to move one of the classes from java to kotlin and at this class I have bottomNavigation menu for navigation. At Java I have such listener:

bottomNavigationView.setOnNavigationItemSelectedListener(menuItem -> {
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            Bundle bundle = new Bundle();
            switch (menuItem.getItemId()) {

                case R.id.full_jobAgent:

                    Objects.requireNonNull(getSupportActionBar()).show();
                    JobList jobList = new JobList();
                    bundle.putInt("offset", 1);
                    jobList.setArguments(bundle);
                    textViewTitle.setText("Jobagent");
                    tvSubTitle.setText(R.string.all_jobs);
                    textViewTitle.setTypeface(custom_font);
                    tvSubTitle.setTypeface(custom_font);
                    disableShowHideAnimation(getSupportActionBar());
                    transaction.replace(R.id.contentContainer, jobList).addToBackStack(null).commit();
                    return true;
                case R.id.received_mess:
                    disableShowHideAnimation(getSupportActionBar());
                    Objects.requireNonNull(getSupportActionBar()).show();


                    MessageList messageList = new MessageList();
                    bundle.putInt("type1", 0);
                    messageList.setArguments(bundle);
                    transaction.replace(R.id.contentContainer, messageList).addToBackStack(null).commit();
                    textViewTitle.setTypeface(custom_font);
                    tvSubTitle.setTypeface(custom_font);
                    textViewTitle.setText(R.string.title_activity_message_center);
                    tvSubTitle.setText(R.string.received);
                    return true;
                case R.id.home_screen:
                    disableShowHideAnimation(getSupportActionBar());
                    Objects.requireNonNull(getSupportActionBar()).hide();
                    transaction.replace(R.id.contentContainer, new PersonalData()).addToBackStack(null).commit();
                    return true;
                case R.id.more:
                    disableShowHideAnimation(getSupportActionBar());
                    textViewTitle.setText(R.string.more_bottom_nav);
                    textViewTitle.setTypeface(custom_font);
                    tvSubTitle.setVisibility(View.GONE);
                    Objects.requireNonNull(getSupportActionBar()).show();
                    transaction.replace(R.id.contentContainer, new MoreScreen()).addToBackStack(null).commit();
                    return true;
                case R.id.notespec:
                    disableShowHideAnimation(getSupportActionBar());
                    textViewTitle.setText(R.string.notepad_bottom_nav);
                    textViewTitle.setTypeface(custom_font);
                    tvSubTitle.setVisibility(View.GONE);
                    Objects.requireNonNull(getSupportActionBar()).show();
                    transaction.replace(R.id.contentContainer, new TestNotepadFragment()).addToBackStack(null).commit();
                    return true;
            }
            return false;
        });

I have rewritten this method to kotlin:

 private val mOnNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item ->
        val transaction = supportFragmentManager.beginTransaction()
        val bundle = Bundle()
        when (item.itemId) {
            R.id.full_jobAgent -> {
                ms.filter_data = HashMap<String, String>()
                disableShowHideAnimation(supportActionBar!!)

                val jobList = JobList()
                bundle.putInt("offset", 1)

                if (intent.getSerializableExtra("filter_data") != null) {
                    bundle.putSerializable("filter_data", intent.getSerializableExtra("filter_data"))
                }
                bottomNavigationView.selectedItemId = R.id.full_jobAgent
                transaction.replace(R.id.contentContainerT, jobList).addToBackStack(null).commit()
                textSetter("Jobagent", resources.getString(R.string.all_jobs))
                //return@OnNavigationItemSelectedListener true
            }

            R.id.received_mess -> {
                disableShowHideAnimation(supportActionBar!!)
                supportActionBar!!.show()
                val messageList = MessageList()
                bundle.putInt("type1", 0)
                messageList.arguments = bundle
                textSetter(resources.getString(R.string.title_activity_message_center), resources.getString(R.string.received))
                transaction.replace(R.id.contentContainerT, messageList).addToBackStack(null).commit()
                //return@OnNavigationItemSelectedListener true
            }

            R.id.home_screen -> {
                disableShowHideAnimation(supportActionBar!!)
                supportActionBar!!.hide()
                transaction.replace(R.id.contentContainerT, PersonalData()).addToBackStack(null).commit()
                //return@OnNavigationItemSelectedListener true
            }

            R.id.notespec -> {
                disableShowHideAnimation(supportActionBar!!)
                supportActionBar!!.show()
                textSetter(resources.getString(R.string.more_bottom_nav), "")
                transaction.replace(R.id.contentContainerT, NotepadScr()).addToBackStack(null).commit()
                //return@OnNavigationItemSelectedListener true
            }

            R.id.more ->{
                disableShowHideAnimation(supportActionBar!!)
                supportActionBar!!.show()
                textSetter(resources.getString(R.string.more_bottom_nav),"")
                transaction.replace(R.id.contentContainerT,MoreScreen()).addToBackStack(null).commit()
                //return@OnNavigationItemSelectedListener true
            }
        }
        false
    }

and my app started to crash. I have found that the problem is here:

return@OnNavigationItemSelectedListener true

some of you will ask me a logs of my app but I couldn't fetch them from logcat because it reloaded very quickly. So... at java we don't have to select the selected item, but at kotlin when I remove wrong line of selection I don't see which item is selected right now. Maybe I did smth wrong?


Solution

  • navigation.setOnNavigationItemSelectedListener(this)
    
    override fun onNavigationItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            R.id.navigation_catalog -> {
                swapFragments(item.itemId, "Catalog")
                return true
            }
            R.id.navigation_shopping_cart -> {
                swapFragments(item.itemId, "ShoppingCart")
                return true
            }
            R.id.navigation_account -> {
                swapFragments(item.itemId, "Account")
                return true
            }
        }
        return false
    }
    
    
    private fun swapFragments(@IdRes actionId: Int, key: String) {
        if (supportFragmentManager.findFragmentByTag(key) == null) {
            savedFragmentState(actionId)
            createFragment(key, actionId)
        }
    }
    
    private fun savedFragmentState(actionId: Int) {
        val currentFragment = supportFragmentManager.findFragmentById(R.id.fragment_home)
        if (currentFragment != null) {
            savedStateSparseArray.put(currentSelectItemId,
                    supportFragmentManager.saveFragmentInstanceState(currentFragment)
            )
        }
        currentSelectItemId = actionId
    }
    
    private fun createFragment(key: String, actionId: Int) {
        when (actionId) {
            R.id.navigation_catalog -> {
                val fragment = CatalogFragment.newInstance(key)
                fragment.setInitialSavedState(savedStateSparseArray[actionId])
                supportFragmentManager.beginTransaction()
                        .replace(R.id.fragment_home, fragment, key)
                        .commit()
            }
            R.id.navigation_shopping_cart -> {
                val fragment = ShoppingcartFragment.newInstance(key)
                fragment.setInitialSavedState(savedStateSparseArray[actionId])
                supportFragmentManager.beginTransaction()
                        .replace(R.id.fragment_home, fragment, key)
                        .commit()
            }
            R.id.navigation_account -> {
                val fragment = AccountFragment.newInstance(key)
                fragment.setInitialSavedState(savedStateSparseArray[actionId])
                supportFragmentManager.beginTransaction()
                        .replace(R.id.fragment_home, fragment, key)
                        .commit()
            }
        }
    
    }