Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I update a View while activity is paused?

Can I update UI / Views (like changing a textviews text) while a activity is paused, for example, because another activity has been started?

I have implemented a little test app that do something like this, by connecting activities with an eventbus.

Activity A has a TextView. Activity A registers itself to the EventBus in onCreate() and unregisters itself in onDestroy(). Activity A starts Activity B. Activity B does some changes and fires a ChangedEvent. This ChangedEvent will be received by Activity A, because its still registered even if Activity A is paused and will update the TextView of Activity A (changing the text).

Everything works fine. I have tested this on various nexus devices and emulators on various Android versions (2.3.3 up to 4.3). No Exceptions or unexpected behavior.

So is it safe to say, I can build an App with an Activity that updates his View while the activity is paused? Does it depends on manufacturer etc? I could test my sample app only on stock android devices.

like image 833
sockeqwe Avatar asked Sep 09 '13 22:09

sockeqwe


People also ask

What is paused activity?

resumed: The activity is in the foreground and the user can interact with it. paused: In this state, the activity is partially obscured by another activity—the other activity that's in the foreground is semi-transparent or doesn't cover the entire screen.

When an activity is in the paused state?

Paused – An activity is in a paused state when the activity is visible but not in focus. Stopped – An activity is in a stopped state when it's no longer visible. It is still in memory, but it could be destroyed by the android runtime, as “stopped” activities are generally considered low priority.

How do I pause app activity?

In an Android device, go to Settings> Google> Personal info & privacy> Activity controls> Web & App Activity and turn off the Web & App Activity.


1 Answers

Yes, you can count on this working.

Views don't actually care about the lifecycle of the Activity that owns their window. (You can add other views directly to the WindowManager outside of an Activity's lifecycle altogether.)

Unless an Activity is actually destroyed (and of course as long as your process isn't killed), a record of its window and views sticks around. If the window is still visible, updates to the views within it will also be visible even if the Activity is paused.

like image 152
adamp Avatar answered Oct 22 '22 09:10

adamp