Buckstabue
Buckstabue

Reputation: 303

Is it safe to call the View.post(runnable) method in the main thread?

I've got a view in a RecyclerView. And I call myView.post(runnable) in onBindViewHolder() method to gather information about myView after the layout pass finishes. Is there any risk of crashing my application if the host activity gets destroyed somehow before my runnable starts?

Upvotes: 0

Views: 506

Answers (1)

Gabe Sechan
Gabe Sechan

Reputation: 93551

In general its safe. However if your onDestroy releases any resources or nulls out any variables that your Runnable depends on you could hit a corner case. For the most part I wouldn't worry about it though. It can't be called after finalize(), because the Runnable will keep a reference to your activity preventing it from being garbage collected until after the Runnable is run. That would be the real problem, but the framework/language prevent it.

Upvotes: 0

Related Questions