Android UI: when can I directly modify a view?
Asked Answered
L

4

10

I have an app with two activities. From the main activity I start a secondary activity using startActivityForResult(). The secondary activity returns data (in the form of an Intent object) to the main activity. On the main activity I have a method onActivityResult() to handle the come back from the secondary activity.

Within this onActivityResult() method, I need to update a View on the main activity (to reflect the new data values). I do not explicitly spawn any threads. My question is: can I directly modify the view from within onActivityResult() method, or do I need to put an event on the UI queue to do it? To be more explicit: can I be sure that the onActivityResult() method is on the UI thread, and in that case can I forget about the UI queue?

Lunetta answered 21/7, 2013 at 16:0 Comment(0)
S
12
  1. Yes, you can modify the view in onActivityResult(). You can modify an Activity's views anytime after you call setContentView() in onCreate(), as long as you are running on the UI thread.

  2. Yes, onActivityResult() is called on the UI thread. This is true for all life cycle methods (onCreate(), onResume(), etc).

Shaeffer answered 21/7, 2013 at 16:31 Comment(0)
O
13

Although onActivityResult is on Ui thread, you may not see your UI updated when modified in onActivityResult. I suspect the reason is redrawing of Ui elements at onResume which forces ui elements to reset by calling resetViews() of ActivityTransitionState at super.onResume().

I faced with this issue while simply updating an EditText inside onActivityResult. EditText was not updated.

The workaround is to save your data in onActivityResult and update Ui at onResume by setting a flag in onActivityResult.

Overdose answered 13/9, 2015 at 15:11 Comment(1)
Thanks alot i was trying to Update my views in onActivityResult but this answer really help me thaks alotDixon
S
12
  1. Yes, you can modify the view in onActivityResult(). You can modify an Activity's views anytime after you call setContentView() in onCreate(), as long as you are running on the UI thread.

  2. Yes, onActivityResult() is called on the UI thread. This is true for all life cycle methods (onCreate(), onResume(), etc).

Shaeffer answered 21/7, 2013 at 16:31 Comment(0)
B
3

The onActivityResult() is executed in the UI thread, you can modify the view on this method.

Bissau answered 21/7, 2013 at 16:10 Comment(1)
Thank you! And after modifying the view, do I need to call invalidate()? I'm a bit confused as to when invalidate() needs to be calledLunetta
U
0

When I try to create and show an AlertDialog on onActivityResult() coming back from making a photo, I get an "android.view.WindowLeaked" error android.view.WindowLeaked:

Activity com...MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{37ac7e30 V.E..... R.....I. 0,0-1272,584} that was originally added here

as soon as I try to show the dialog.

So I believe it's not always OK to assume that OnActivityResult() runs on the main thread.

Unadvised answered 6/7, 2015 at 6:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.