Managing executors with fragments

41 views Asked by At

I'm developing an app that performs tests on mobile network. The app has different Fragment (Home, Dashboard and Notification).

The Home fragment is able to start a periodic test using the Executors class. There is an image "Off" displayed on the screen. If I click on it, the image becomes "On" and the tests start (startTests.scheduleAtFixedRate(st,0,5, TimeUnit.SECONDS)).

When I go to another screen another fragment is displayed, but the tests keep being executed. When I come back to Home fragment, I get the "Off" image again, even if the tests keeps being executed. If I click on the image, it becomes "On" and another instance of Executors is created, so I have two tests being performed at the same time. That's why changing Fragment makes the Home fragment being started as it was the first time it is displayed.

I could stop the executors when the fragment changes (overriding onSaveInstanceState), but I don't want to stop the tests, I'd like to keep the tests being performed and I'd like to have the image "On" being displayed when I come back to the Home fragment.

I could create a variable TEST_STARTED to be saved in onSaveInstanceState saying that the tests have been started, so when I come back to the Home fragment I know that the tests are going on. But how can I manage the Executor? I need to be able to stop the Executor when I click on the image.

package com.giorgio.roamingqosapp.ui.home

import ...

val TAG = ""
@SuppressLint("StaticFieldLeak")
private lateinit var interrOnImageView : ImageView
@SuppressLint("StaticFieldLeak")
private lateinit var interrOffImageView : ImageView
private var startTests = Executors.newSingleThreadScheduledExecutor()
private lateinit var testProvider: ScheduledFuture<*>


class HomeFragment : Fragment() {

    private var _binding: FragmentHomeBinding? = null
//    val sm = getContext()?.let { getSystemService(it,SmsManager::class.java) }

    // This property is only valid between onCreateView and
    // onDestroyView.
    private val binding get() = _binding!!

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        val homeViewModel =
            ViewModelProvider(this).get(HomeViewModel::class.java)
        var text = ""

        _binding = FragmentHomeBinding.inflate(inflater, container, false)
        val root: View = binding.root

        /*
        val textView: TextView = binding.textHome
        homeViewModel.text.observe(viewLifecycleOwner) {
            textView.text = it
        }
        */

        val tm = requireActivity().getSystemService(Activity.TELEPHONY_SERVICE) as TelephonyManager
        val SIMOperatorName = tm.simOperatorName
        val isRoaming = tm.isNetworkRoaming

        if (SIMOperatorName != null) {
            text = getString(R.string.registered_operator, SIMOperatorName)
            binding.registeredOperator.text = text
        }
        text = when (isRoaming) {
            true -> getString(R.string.roaming, "Roaming")
            false -> getString(R.string.roaming, "Home")
        }
        binding.roaming.text = text

        val ledImageView: ImageView = binding.registeredImageView
        ledImageView.setImageDrawable(AppCompatResources.getDrawable(requireContext(),R.drawable.led_verde))

        interrOffImageView = binding.testsOn
        interrOffImageView.setImageDrawable(AppCompatResources.getDrawable(requireContext(),R.drawable.off))
        interrOnImageView = binding.testsOff
        interrOnImageView.setImageDrawable(AppCompatResources.getDrawable(requireContext(),R.drawable.on))
        interrOnImageView.visibility = View.GONE
        interrOnImageView.setOnClickListener { startStopTest() }
        interrOffImageView.setOnClickListener { startStopTest() }

        return root
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }

    private fun startStopTest() {
        Log.i(TAG,"Check OnOff button image")

        if (interrOnImageView.visibility==View.VISIBLE) {
            interrOnImageView.visibility = View.GONE
            interrOffImageView.visibility = View.VISIBLE
            Log.i(TAG,"On -> Off")
            testProvider.cancel(true)
            Toast.makeText(context, getString(R.string.show, "Fermato il test periodico"), Toast.LENGTH_SHORT).show()
        }
        else {
            interrOffImageView.visibility = View.GONE
            interrOnImageView.visibility = View.VISIBLE
            Log.i(TAG,"Off -> On")
            val st = ScheduledTests()
            testProvider = startTests.scheduleAtFixedRate(st,0,5, TimeUnit.SECONDS)
            Toast.makeText(context, getString(R.string.show, "Avviato il test periodico"), Toast.LENGTH_SHORT).show()
        }
    }

}
0

There are 0 answers