Lucy
Lucy

Reputation: 121

Tap back button twice to terminate application in Fragment

I am implementing a function which is to close the application by clicking back button twice. It shows the toast message when it is first clicked. However, the problem is it shows infinite loop error then is termiated. I do not really understand the reason after two days of browsing. Any help will be greatly appreciated.

My fragment code

    class RegisteredMainFragment : Fragment() {

    private lateinit var binding : FragmentRegisteredMainBinding
    private val viewModel : RegisteredMainViewModel by inject()

    private lateinit var mContext: MainActivity

    override fun onAttach(context: Context) {
        super.onAttach(context)
        mContext = context as MainActivity
    }

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

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

        binding.viewModel = viewModel
        binding.lifecycleOwner = viewLifecycleOwner
      
        onBackPressedCallback()
    }

    private fun onBackPressedCallback() {
        requireActivity().onBackPressedDispatcher.addCallback(viewLifecycleOwner,
            object : OnBackPressedCallback(true ) {
                override fun handleOnBackPressed() {
                    if (doubleBackToExitPressedOnce) {
                        requireFragmentManager().popBackStack()
                        return
                    }
                    doubleBackToExitPressedOnce = true
                    Toast.makeText(requireContext(), "click back button again", Toast.LENGTH_SHORT).show()
                    Handler().postDelayed({ doubleBackToExitPressedOnce = false }, 2000)
            }
        })
    }

    override fun onResume() {
        super.onResume()
        viewModel.onAuthExist()
    }

    override fun onDestroyView() {
        super.onDestroyView()
        PushObserverService.unregisterObserver(this)
    }
}

Upvotes: 1

Views: 1668

Answers (1)

Lucy
Lucy

Reputation: 121

I solved the problem by writing it in override fun onAttach Please check the code below

    override fun onAttach(context: Context) {
    super.onAttach(context)
    mContext = context as MainActivity

    callback = object : OnBackPressedCallback(true) {
        override fun handleOnBackPressed() {
            if (doubleBackToExitPressedOnce) {
                activity?.finish()
            }
            doubleBackToExitPressedOnce = true
            Handler().postDelayed({ doubleBackToExitPressedOnce = false }, 2000)
            showSnackBar(
                context = mContext,
                layout = binding.layoutMain,
                type = Constants.SnackBarTypes.Warn,
                message = mContext.getString(R.string.tap_twice_to_terminate)
            )
        }
    }
    requireActivity().onBackPressedDispatcher.addCallback(this, callback)
}

Upvotes: 1

Related Questions