简体   繁体   中英

I need to constantly send data from one activity to another in Android

In my MainActivity I have the bluetooth connection process happening and I'd like to constantly send the data arriving to another Activity ( UI ), so that it can be properly displayed to the user.

I was able to "sort of" do it in the following manner, but it is not exactly what I'm looking for, since the UI activity gets restarted every time I have new data incoming, and therefore the "activity starting" animation happens everytime. This update shouldn't be noticeable for the user. Is there any way I can use Intents to pass the data I need constantly from MainActivity to UI without restarting UI everytime?

How it is working as of now, with the undesirable UI restarting:

On the first activity ( MainActivity ):

  Intent UIdata = new Intent(MainActivity.this, UI.class);
  UIdata.putExtra("data", finalData);
  startActivity(UIdata);

And on the second activity ( UI ):

  Bundle dataIn = getIntent().getExtras();
  if (dataIn != null){
    String UIdataIn = dataIn.getString("data");
  }

Thanks!

Att Lang

What you want to do is logically flawed. An Activity is only "active" while it's in the front. If you need something which send/receives data continuously even when in background you need to user a Service .

From there you have multiple possibilities to updated the UI of your running activity, for example:

1) The service can broadcast intents and the activity can have a listener which listens to intents

2) You can bind an activity to a service and then use methods/variables defined in the service.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM