0

I have an activity A which has socket communication, and I'm opening activity B on click of a button with intent from Activity A.

        public void onClick(View v) {
                    //open preferences activity
                    intent = new Intent(MainActivity.this, PreferencesActivity.class);
                    //using some bundle here to send data
                    .....
                    MainActivity.this.startActivityForResult(intent, 1);
               }

Now, if at all any socket communication happens in activity A then I have to identify that (some communication happened in activity A) in activity B and need to close activity B and bring the activity A to foreground as well. Socket communication is as follows.

 ConnectSocket();
 while (true) {
        //wait for socket client
        socClient = (SSLSocket) sslServerSocket.accept();
        //once socket is connected send it back to activity
        if (socClient != null) {
            if (socketThread != null) {
                   connectedSocket = socClient;
                   socClient = null;
                   communicationThread = new ReadSocketThread();
                   communicationThread.start();
            }
        }
     }
  }               

Here ReadsocketThread() is a thread that read the socket for messages.

How to notify activity B that something happened in activity A. I tried using handler to send message but not successful as handler in activity A never sends message to activity B. As a beginner, may be I am not using properly.

dev_P
  • 73
  • 10
  • 1> use local broadcast manager.- register broadcast in one activty and send broadcast from other activity. 2> use bound service. – Keyur Thumar Jan 18 '17 at 04:34
  • thanks for the reply.. i used localbroadcastmanager is able to communicate... a good link with sample code is found here... http://stackoverflow.com/questions/8802157/how-to-use-localbroadcastmanager – dev_P Jan 18 '17 at 05:12
  • Good , make sure you are not updating any UI after activity onstop system callback – Keyur Thumar Jan 18 '17 at 06:58

2 Answers2

0

A workaround approach is to use EventBus in Android. To use EventBus:

compile 'org.greenrobot:eventbus:3.0.0'

Then, create an event to receive message from Activity A

    public class HelloWorldEvent {
    private final String message;

    public HelloWorldEvent(String message) {
        this.message = message;
    }

    public String getMessage() {
        return message;
    }
}

Then create a sender from Activity A :

EventBus.getDefault().post(new HelloWorldEvent("Hello EventBus!”);

Then, you can add as many listeners as you require, as in your case , in Activity B:

    @Subscribe
    public void onEvent(HelloWorldEvent event){
  // your implementation
  Toast.makeText(getActivity(), event.getMessage(), Toast.LENGTH_SHORT).show();
}

So, what happens here is whenever EventBus.getDefault().post(new HelloWorldEvent("Hello EventBus!”); is invoked from Activity A, you'd get the Toast in Activity B, you can pass your preferred message there. A more detailed example on EventBus is available [here]

OBX
  • 6,044
  • 7
  • 33
  • 77
0

I found another simple solution for my scenario as I do not need to pass any message between activities, instead on socket communication want to close activity B and bring activity A to foreground. I achieved this without using any broadcast or event bus by just opening the Activity B with

 startActivityForResult(intent, somerequestcode);

and when there is communication to activity A, at the appropriate position simply call

finishActivity(somerequestcode);

note : somerequestcode can be any integer

dev_P
  • 73
  • 10