0

Let's say that I've the following main activity:

public class MwConsoleActivity extends Activity {

    private classChild child = null;    

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        child = new classChild();
    }
}

Then consider the implementation of the class "classChild":

public class MwBondingAgent extends SapereAgent {

    MwBondingAgent(){}

    public void AddEventListener(childAddedEvent event) {

       //Send the data of event back to the main activity

    }
}

I've tried to use IntentServices but was not able to receive the values back to the main activity. What would be the approach I've to take?

Cheers Ali

Michal Klouda
  • 14,263
  • 7
  • 53
  • 77
don ali
  • 157
  • 2
  • 16
  • i'm not sure exactly what you are asking. are you asking how to implement the observer pattern using a listener? – toadzky Oct 31 '12 at 14:26
  • Thank you guys for your replies.The solution with design pattern is very clean. But I managed to solve this problem by IntentService and ResultReceiver which is much more better than IntentFilters. The link provided below helped me a lot: http://stackoverflow.com/questions/3028306/download-a-file-with-android-and-showing-the-progress-in-a-progressdialog/3028660#3028660 – don ali Nov 01 '12 at 18:19

3 Answers3

2

You can use and intentFilter to listen for broadcasts. Add this to the activity:

 IntentFilter intentFilter = new IntentFilter(
                "com.unique.name");
        mReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                //extract our message from intent
                String msg_for_me = intent.getStringExtra("some_msg");
             }
        };
        //registering our receiver
        this.registerReceiver(mReceiver, intentFilter);

In your class add this to the part you want to notify the activity:

Intent i = new Intent("com.unique.name").putExtra("some_msg", "I have been updated!");
this.sendBroadcast(i);
Gabriel Netto
  • 1,818
  • 1
  • 16
  • 26
1

You should use the observer / listener pattern.

http://www.vogella.com/articles/DesignPatternObserver/article.html

It is one of the most used design patterns when using MVC architecture pattern.

Fabian Knapp
  • 1,342
  • 13
  • 27
1

Your question is quite unclear but I think what you are wanting is to implement a callback to your activity. You can do this using an interface.

public class MwConsoleActivity extends Activity implements MwBondingAgent{

    private classChild child = null;    

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        child = new classChild();
    }

    @Override
    public void gotEventData(EventData myEventData) {

        //to whatever you want with myEventData
    }
}

And in your other class.

public class MwBondingAgent extends SapereAgent {

    private MwBondingAgentCallback activityCallback;

    MwBondingAgent(Activity callback){

         activityCallback = callback;
    }

    public void AddEventListener(childAddedEvent event) {

        //Send the data of event back to the main activity
        EventData myEventData = //got some event data;
        //Send it back to activity
        activityCallback.gotEventData(myEventData);
    }

    public interface MwBondingAgentCallback {

        public void gotEventData(EventData myEventData);
    }
}
James McCracken
  • 15,488
  • 5
  • 54
  • 62