Hello all
This simple post tells you how to pass an object to next activity and pass it back when that activity is closed.
Check these posts before reading this post to get an overview of other methods.
1. How to pass an Object from One Activity to another in Android?
2. How to pass an arraylist value from one activity to another in android?
Note : The most important thing to note in this post is that for an object to be passed to an activity or back, the Object class should implement Serializable Interface.
Here MyObject is My class whose object is I am going to pass.
And the MyObject class should look like this.
MyObject.java
package com.example.passobjectback; import java.io.Serializable; public class MyObject implements Serializable { /** * */ private static final long serialVersionUID = 1L; String name, website; public MyObject() { } public String getWebsite() { return website; } public void setWebsite(String website) { this.website = website; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
This is my first activity that is sending MyObject to the Second Activity.
MainActivity.java
package com.example.passobjectback; import android.app.Activity; import android.content.Intent; import android.os.Bundle; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final MyObject myobj = new MyObject(); myobj.setName("Coderz"); myobj.setWebsite("CoderzHeaven.com"); Intent intent = new Intent(MainActivity.this, Second.class); intent.putExtra("myobject", myobj); startActivityForResult(intent, 1); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == 1) { if (resultCode == Activity.RESULT_OK) { Bundle b = data.getExtras(); if (b != null) { MyObject myobj = (MyObject) b.getSerializable("Obj"); System.out.println("Name : " + myobj.getName() + ", Website : " + myobj.getWebsite()); } } else if (resultCode == 0) { System.out.println("RESULT CANCELLED"); } } } }
Note that here I am starting my second activity directly but using “startActivityForResult” because I am expecting something back from the Second Activity when it closes.
So we should have this method in the MainActivity to get the results.
protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); }
This is my Second Activity that is accepting my object and sending back the same object which is received in the intent in the “onActivityResult” function in the MainActivity.
Second.java.
package com.example.passobjectback; import android.app.Activity; import android.content.Intent; import android.os.Bundle; public class Second extends Activity { MyObject myobj; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); this.setTitle("Second Activity"); Bundle b = getIntent().getExtras(); if (b != null) { myobj = (MyObject) getIntent().getExtras() .getSerializable("myobject"); } } @Override public void onBackPressed() { Intent intent = new Intent(); intent.putExtra("Obj", myobj); setResult(Activity.RESULT_OK, intent); finish(); super.onBackPressed(); } }
I am getting the object back in the “onActivityResult” in the MainActivity when we press BackButton in the second Activity.
This is how we send back the object in the backpressed function
@Override public void onBackPressed() { Intent intent = new Intent(); intent.putExtra("Obj", myobj); setResult(Activity.RESULT_OK, intent); finish(); super.onBackPressed(); }
The main thing to note is that if you are closing the activity using back button then you should write the setResult() before “super.onBackPressed();” statement.