@@ -25,6 +25,7 @@ import com.omarea.krscript.model.RunnableNode
2525import com.omarea.krscript.model.ShellHandlerBase
2626import android.content.DialogInterface
2727import com.tool.tree.R
28+ import java.lang.ref.WeakReference
2829
2930class DialogLogFragment : DialogFragment () {
3031
@@ -39,6 +40,9 @@ class DialogLogFragment : DialogFragment() {
3940 private var params: HashMap <String , String >? = null
4041 private var themeResId: Int = 0
4142 private var onDismissRunnable: Runnable ? = null
43+
44+ // Lưu tham chiếu Handler để giải phóng khi hủy View
45+ private var currentHandler: MyShellHandler ? = null
4246
4347 override fun onCreateView (
4448 inflater : LayoutInflater ,
@@ -136,8 +140,7 @@ class DialogLogFragment : DialogFragment() {
136140
137141 binding.actionProgress.isIndeterminate = true
138142
139- return MyShellHandler (requireContext().applicationContext, object : IActionEventHandler {
140- // Thay chỗ này trong openExecutor -> onCompleted()
143+ val handler = MyShellHandler (requireContext().applicationContext, object : IActionEventHandler {
141144 override fun onCompleted () {
142145 running = false
143146 onExit.run ()
@@ -171,6 +174,9 @@ class DialogLogFragment : DialogFragment() {
171174 }
172175 }
173176 }, binding.shellOutput, binding.actionProgress)
177+
178+ this .currentHandler = handler
179+ return handler
174180 }
175181
176182 @FunctionalInterface
@@ -182,11 +188,14 @@ class DialogLogFragment : DialogFragment() {
182188
183189 class MyShellHandler (
184190 context : Context ,
185- private var actionEventHandler : IActionEventHandler ,
186- private var logView : TextView ? ,
187- private var shellProgress : ProgressBar ?
191+ private var actionEventHandler : IActionEventHandler ? ,
192+ logView : TextView ? ,
193+ shellProgress : ProgressBar ?
188194 ) : ShellHandlerBase(context) {
189195
196+ private val logViewRef = WeakReference (logView)
197+ private val progressRef = WeakReference (shellProgress)
198+
190199 private val errorColor = getColor(R .color.kr_shell_log_error)
191200 private val basicColor = getColor(R .color.kr_shell_log_basic)
192201 private val scriptColor = getColor(R .color.kr_shell_log_script)
@@ -201,6 +210,12 @@ class DialogLogFragment : DialogFragment() {
201210 }
202211 }
203212
213+ fun release () {
214+ logViewRef.clear()
215+ progressRef.clear()
216+ actionEventHandler = null
217+ }
218+
204219 override fun handleMessage (msg : Message ) {
205220 when (msg.what) {
206221 EVENT_EXIT -> onExit(msg.obj)
@@ -218,17 +233,20 @@ class DialogLogFragment : DialogFragment() {
218233 updateLog(msg, errorColor)
219234 }
220235
221- override fun onStart (forceStop : Runnable ? ) = actionEventHandler.onStart(forceStop)
236+ override fun onStart (forceStop : Runnable ? ) {
237+ actionEventHandler?.onStart(forceStop)
238+ }
222239
223240 override fun onProgress (current : Int , total : Int ) {
224- shellProgress?.post {
241+ val shellProgress = progressRef.get() ? : return
242+ shellProgress.post {
225243 when {
226- current < 0 -> shellProgress? .apply {
244+ current < 0 -> shellProgress.apply {
227245 visibility = View .VISIBLE
228246 isIndeterminate = true
229247 }
230- current >= total -> shellProgress? .visibility = View .GONE
231- else -> shellProgress? .apply {
248+ current >= total -> shellProgress.visibility = View .GONE
249+ else -> shellProgress.apply {
232250 visibility = View .VISIBLE
233251 isIndeterminate = false
234252 max = total
@@ -238,30 +256,30 @@ class DialogLogFragment : DialogFragment() {
238256 params.topMargin = 44
239257 layoutParams = params
240258 }
241- requestLayout()
242259 }
243260 }
244261 }
245262 }
246263
247264 override fun onStart (msg : Any? ) {
248- logView ?.text = " "
265+ logViewRef.get() ?.text = " "
249266 }
250267
251268 override fun onExit (msg : Any? ) {
252269 val code = (msg as ? Int ) ? : - 1
253270 if (! hasError && code == 0 ) {
254- actionEventHandler.onSuccess()
271+ actionEventHandler? .onSuccess()
255272 }
256273 updateLog(context.getString(R .string.kr_shell_completed), endColor)
257- actionEventHandler.onCompleted()
274+ actionEventHandler? .onCompleted()
258275 }
259276
260277 override fun updateLog (msg : SpannableString ? ) {
278+ val logView = logViewRef.get() ? : return
261279 msg?.let {
262- logView? .post {
263- logView? .append(it)
264- (logView? .parent as ? ScrollView )?.fullScroll(ScrollView .FOCUS_DOWN )
280+ logView.post {
281+ logView.append(it)
282+ (logView.parent as ? ScrollView )?.fullScroll(ScrollView .FOCUS_DOWN )
265283 }
266284 }
267285 }
@@ -293,6 +311,8 @@ class DialogLogFragment : DialogFragment() {
293311 }
294312
295313 override fun onDestroyView () {
314+ currentHandler?.release()
315+ currentHandler = null
296316 super .onDestroyView()
297317 _binding = null
298318 }
@@ -321,4 +341,4 @@ class DialogLogFragment : DialogFragment() {
321341 return fragment
322342 }
323343 }
324- }
344+ }
0 commit comments