Vishal Solanki
Vishal Solanki

Reputation: 21

Refreshing activity without Intent

I have 3 Activities. Intent is passed from Activity 1 to Activity 2 with some data which is to be used for DB transactions. From Activity 2 data and Intent are passed to Activity 3.

Now, i want to transfer from Activity 3 to Activity 2 but as activity 2 gets intent from Activity 1 , it returns some error which results in null exceptions :/

So , i want to refresh activity 2 on returning from Activity 3 , but without intent or proper use of intent which does not affect data

Upvotes: 0

Views: 1810

Answers (2)

You can use interfaces to achieve this. Example: in your Activity3, create an interface:

interface Communicator {
    void receiveData(Object toSend);
}

Then you implement your Activity2 as follows:

public class Activity2 extends Activity implements Activity3.Communicator, Serializable {

    //You'll have to implement the method
    public void receiveData(Object toSend) {
         //do what you have to do with your object once it's been sent back.
    }
}

In order to use this pattern, you'll have to use an intent to start Activity3 like you did. I used Serializable because you want to pass an instance of the Activity through the Intent:

 Intent intent = new Intent(this, Activity3.class);
 intent.putExtra("activity2", this);
 startActivity(intent);

You retrieve the Activity2 object in Activity3 in its onCreate method:

public class Activity3 extends Activity {
    private Communicator mActivity2; //your activity
    private Object mData; //Whatever object you want to send

    public void onCreate(Bundle bundle) {
         //...
         Intent intent = getIntent();
         Serializable object = intent.getSerializableExtra("activity2");
         if (object != null && object instanceof Communicator) {
               //make sure the object is not null and implements the Communicator
               mActivity2 = (Communicator) object;
         }
    }
}

Once you are done in your Activity3 (once it is closed), you implement a method like onBackPressed or onDestroy as follows:

 @Override
 public void onDestroy() { //better if the back button is not pressed
     super.onDestroy();
     if (mActivity2 != null) { //check if is not null
         mActivity2.receiveData(mData);
     }
 }

And your Activity should be updated without using intents.

By the way, using Fragments using this pattern is MUCH better than Activities. You should use fragments instead.

Upvotes: 0

Arpit Patel
Arpit Patel

Reputation: 8057

Start Activity2 from Acivity1 as:

Intent i = new Intent(this,  Activity2.class);
startActivityForResult(i, 1);

in Activity2 use setResult for sending data back :

Intent intent = new Intent();
intent.putExtra("edittextvalue","value_here")
setResult(RESULT_OK, intent);        
finish();

and in First Activity receive data as onActivityResult:

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 1) {
         if(resultCode == RESULT_OK){
             String stredittext=data.getStringExtra("edittextvalue");
         }     
    }
} 

OR

You can use this In Activity2,

@Override
public void onBackPressed() {
    String data = mEditText.getText();
    Intent intent = new Intent();
    intent.putExtra("MyData", data);
    setResult(resultcode, intent);
}

In Activity1,

onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 1) {
        if(resultCode == RESULT_OK) {
            String myStr=data.getStringExtra("MyData");
            mTextView.setText(myStr);
        }
    }
}

OR

you can use SharedPreferences also for Sharing data Between Activities

Upvotes: 1

Related Questions