mohammedali sunasara
mohammedali sunasara

Reputation: 220

Send Data to another activity after the data gets updated in android

i have two activities activity 1 and activity 2 in android. From activity 1, I want to send data to activity 2. My activity 1 gets updated anytime when activity is minimized then also. I want to send the updated data to another activity 2 anytime time when data in activity 1 gets updated....

Upvotes: 0

Views: 101

Answers (2)

kevinkl3
kevinkl3

Reputation: 961

You could simply use a CallBack and a Singleton, the idea is this:

public class Mediator{

    static MyCallBack mCallBack;

    public static void setCallback(MyCallBack cb){
        mCallBack = cb;
    }

    public static void callCallBack(Object data){
        if(mCallBack != null){
            mCallBack.onUpdate(data);
        }
    }

    public static interface MyCallBack{
        public void onUpdate(Object data);
    }
}

first extends Activity{
    //methods and fields
    //...
    private void update(){
        //some logic...
        String someData = "The data";
        Mediator.callCallBack(someData);
    }
}

second extends Activity{

    public void onCreate(){
        //...
        Mediator.setCallback(new Mediator.MyCallBack(){
            public void onUpdate(Object data){
                //LOGIC WITH DATA HERE
            }
        });
    }
    //methods and fields
    //...
}

Upvotes: 0

Jatin
Jatin

Reputation: 1670

You can send data using handler to another activity. Step 1:

//-- Create Handler in destination activity.

public static Handler handler;

handler=new Handler(new Handler.Callback() {

    @Override
    public boolean handleMessage(Message msg) {
        // TODO Auto-generated method stub

        //-- retreiving data
        String data=msg.obj.toString();
        int i=msg.arg1;

        return false;
    }
});

Step 2:

//-- Passing data from source activity.

Message msg=new Message();
msg.arg1=10;//Pass int value
msg.obj="Test Message";//Pass any type of value
DestinationActivity.handler.sendMessage(msg);// DestinationActivity is your activity from u want to pass data.

Upvotes: 1

Related Questions