Z
Z
Zenx2015-01-07 23:27:29
Android
Zenx, 2015-01-07 23:27:29

How to update data in an Activity in Android on an event?

The MainActivity has a built-in timer that receives data every second as a separate thread and sends it to the second activity as an intent. In the second activity in onCreate(), the data is retrieved, but only once. How to update data in second activity every second?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
O
one pavel, 2015-01-08
@Zenx

Let's talk =) There is an object that sends a message or some kind of notification by timer.
And we want to catch this message. The intent is not suitable for us, it is boring, once the activity has been started and blown away.
So we need a mechanism for constant notification. So, in order for the message to come out to us, we need to tell someone that we would like to receive an alert. That is to subscribe. I offer two options.
1. directly to the object with the timer to say that such and such an object wants to receive a message,
for this there is an Observer-Observable
pattern cpp-reference.ru/patterns/behavioral-patterns/observer
developer.android.com/reference/java/util/Observer .html
www.vogella.com/tutorials/DesignPatternObserver/ar...
2. subscribe through the third object, which will resolve
the reception and sending of notifications
cpp-reference.ru/patterns/behavioral-patterns/mediator
there are several options for
the standard android mechanism
developer.android.com/reference/android/support/v4...
third-party libraries
https://github.com/greenrobot/EventBus
square.github.io/otto
I think you understand that the activity itself should not subscribe to the event
and if messages come in the background thread, then use
runOnUiThread() for the activity or post() for View objects
something like this

O
Oleg Gamega, 2015-01-08
@gadfi

the first one was already closed by that time, I don’t know your task, but look towards services or AsyncTask Threat
global variables are evil, there is a serious suspicion that the architecture should be changed

I
IceJOKER, 2015-01-08
@IceJOKER

every second to open the activation over and over again? O_O what a perversion, is it really impossible to do it in another way?
can the data be sent only once? without the code, they are unlikely to help you

Z
Zenx, 2015-01-08
@Zenx

No, the activity to open is definitely a perversion. In the second activity, I check the global variable every second and update the UI.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question