Android Menu Item Navigate Crash

48 Views Asked by At

When I go to the main fragment from the login fragment for the first time and press the logout in the menu item in this fragment, I return to the login fragment again. However, when I go to the main fragment from the login fragment for the second time and press the logout button in the menu item again, I get this error. I couldn't understand what the problem is. Why is it throwing an error in the second loop?

** java.lang.IllegalStateException: Fragment MainFragment{632a8a3} (33a94b4d-9e85-4315-ae99-51e5edc7ddc2 id=0x7f08015e) did not return a View from onCreateView() or this was called before onCreateView().**


class LoginFragment : Fragment() {
    private lateinit var binding : FragmentLoginBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        auth = Firebase.auth
        database = Firebase.database.reference
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = FragmentLoginBinding.inflate(inflater,container,false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        binding.apply {
            btnConfirm.setOnClickListener {
                     Navigation.findNavController(requireView()).navigate(R.id.action_loginFragment_to_mainFragment)

        }
    }

}

class MainFragment : Fragment() {
    private lateinit var binding : FragmentMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)


    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {


        binding = FragmentMainBinding.inflate(inflater, container, false)
        return binding.root
    }


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
 



        val toolbar = binding.toolbar
        (requireActivity() as AppCompatActivity).setSupportActionBar(toolbar)
        requireActivity().addMenuProvider(object : MenuProvider {
            override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) {
                if(menu.size()==0){
                    menuInflater.inflate(R.menu.main_menu,menu)
                }
            }

            override fun onMenuItemSelected(menuItem: MenuItem): Boolean {
                return when (menuItem.itemId) {
                    R.id.logout ->{
                Navigation.findNavController(requireView()).navigate(R.id.action_mainFragment_to_loginFragment)
                        true
                    }
                    else -> false
                }
            }

        })



    }

}
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/nav_menu"
    app:startDestination="@id/splashFragment">
    <fragment
        android:id="@+id/loginFragment"
        android:name="com.yasinsenel.yapacaklarm.view.fragment.LoginRegisterFragment"
        android:label="fragment_login"
        tools:layout="@layout/fragment_login" >
        <action
            android:id="@+id/action_loginFragment_to_mainFragment"
            app:destination="@id/mainFragment"
            app:enterAnim="@anim/nav_default_enter_anim"
            app:exitAnim="@anim/nav_default_exit_anim" />
    </fragment>

    <fragment
        android:id="@+id/mainFragment"
        android:name="com.yasinsenel.yapacaklarm.view.fragment.MainFragment"
        android:label="fragment_main"
        tools:layout="@layout/fragment_main" >
        <action
            android:id="@+id/action_mainFragment_to_loginFragment"
            app:destination="@id/loginFragment" />
</navigation>
1

There are 1 best solutions below

2
triay0 On

have yot tried setting _binding = null in OnDestroyView

maybe previous binding instance stills in memory