Issue
can android UI elements be updated after onStop has been called but activity still in the stack. Lets say user navigates away from the activity by pressing the home button, but a thread is still running. 2 minutes later it updates UI elements, they seem to update per my tests. but i a colleague of mine is arguing against this as it can cause crashes. What do you all usually do in this case ?
Some people would stop the network call on onStop and others just let it continue and would only release in onDestroy. what is the best practice here.
i've already done a quick test and UI is updating in onStop (maybe its batched until foreground again , not sure how it works):
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_wave)
Handler().postDelayed({motion_layout.setBackgroundColor(ContextCompat.getColor(this,R.color.green))},9000)
//i press the home button and wait 9 seconds, i return to activity and the color does change
}
}
Solution
I see no reason why this would not work. As long as you update the UI elements from the main (UI) thread, you can do this at any time up until onDestroy()
is called. There should be no reason for a crash.
Answered By - David Wasser
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.