RaPthorne February 2016

How to send string from an activity to a thread called in main activity [ANDROID]

I'm developing a bluetooth chat for android. This is the problem:

My main activity A starts a thread to manage bt connection, and then A starts a new activity B. I must know a way to send String continually from the activity B to the thread called in main activity A. How can I do it?

Answers


Diyoda February 2016

If you directly start a thread, then there is no way of accessing that thread from another activity and may cause memory leaks.

Your best option is to have separate Service to manage Bluetooth connection.

You can have a separate Service and initiate the tread form the Service (Service runs in the main thread).

And You have to be careful with the starting and stopping of the Service.

There are 2 ways to start the Service,

You can start a Service or you can bind to a Sstrong textervice.

In your case you will have to start the service in can bind to the service form both Activity A and Activity B, or you can start the service in Activity A and bind to it from Activity B.

But only thing you will have to be careful is when to stop the service. otherwise you will have memory leaks.

Once the service is up and running you can communicate with it from-to your Activity. and you can pass what ever information to the tread running in the Service component.

You can read on this in processes-and-threads and services


Rakesh February 2016

You can do it via Bind and Callback interface. Please refer old thread.

Communication between Activity and Service

Post Status

Asked in February 2016
Viewed 1,703 times
Voted 9
Answered 2 times

Search




Leave an answer