计时器未取消

时间:2019-10-28 13:21:26

标签: android kotlin timer timertask

这个问题的其他情况都没有解决我的问题。我有一个片段出现在事务序列的末尾。它旨在在其中包含的 <div class="row"> <div class="col-md-3 mb-3"> <label for="pi-ida">Composição de Produtos Internos</label> <select name="estrutura" multiple id="id_estrutura" required> {%for prod in lista_pi%} <option value="{{prod.id}}">{{prod.produto_desc}}</option> {% endfor %} </select> </div> </div> <hr class="mb-4"> <button class="btn btn-primary btn-lg btn-block" type="submit">Cadastrar</button> 完成时关闭该应用:

Timer

}

每当片段离开时,都会调用

var terminalTimer: Timer? = null class TerminalFragment : Fragment() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) } override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_terminal, container, false) } override fun onStart() { super.onStart() initUi() startCountDown() } override fun onStop() { super.onStop() AppLog.i(TAG, "onStop()") stopCountDown() } private fun startCountDown() { if (terminalTimer == null) { terminalTimer = Timer() terminalTimer!!.schedule(object : TimerTask() { override fun run() { AppLog.i(TAG, "Terminal countdown finished") { activity?.finish() } }, 5000) } } private fun stopCountDown() { AppLog.i(TAG, "stopCountDown()") terminalTimer?.cancel() terminalTimer?.purge() terminalTimer = null } private fun returnToStart() { AppLog.i(TAG, "returnToStart()") (context as MainActivity).restartFlow() // calls popBackStackImmediate() for every fragment in the backstack, returning user to the beginning of their flow } companion object { @JvmStatic fun newInstance(terminalType: String, amountLoaded: Double) = TerminalFragment().apply { arguments = Bundle().apply { } } } ,但有时它会以某种方式保留下来并从另一个Fragment中关闭应用程序。使用日志,我还发现有时该计时器似乎有2个实例。我如何确保此倒计时在此片段之外永远不会活动,并且在片段的onStop()中被取消/销毁?

0 个答案:

没有答案