Android: How to send interface from one activity to another Android: How to send interface from one activity to another android android

Android: How to send interface from one activity to another


First and foremost, this is very bad:

this.inter = (MyInterface) this.context;

If you pass a context into the constructor which does not implement your interface your application will crash and it's easy to make such a mistake. So you see, this is very error prone, instead implement it like this:

public class MyObject {    private Context context;    private MyInterface inter;    public MyObject(Context context, MyInterface inter) {        this.context = context;        this.inter =  inter;        inter.aMethod();    }}

This way it's much safer, and cleaner.


To send the Object to another Activity make sure it implements Serializable like this:

public interface MyInterface extends Serializable {    public void aMethod();}

And now you can just add the interface as an extra to the Intent which starts the other Activity:

Intent intent = new Intent(context, OtherActivity.class);intent.putExtra("interface", inter);startActivity(intent);

And in the OtherActivity you can get the Object from the Intent like this:

@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    Intent intent = getIntent();    MyInterface inter = (MyInterface) intent.getSerializableExtra("interface");    ...}

EDIT:

When you do something like this in your Activity you are creating an anonymous class:

OnCreateListener inter = new OnCreateListener() {    @Override    public void onObjCreate() {        Log.d("pltk", "dfgbfdgh");    }};

The thing about such an anonymous class is that they are not static, meaning you can still access methods and variables from the class in which this listener is nested. Internally the reason for this is that this new class keeps a reference to the instance of the class which created it, in your case the enclosing Activity. This is a great thing and we use it all the time for OnClickListener etc. But in your case it is a problem because you want to send this Object to another Activity. The internal reference to the old Activity keeps it from being serialised and that is a good thing. If you could just send an Object like that you would create memory leaks like crazy because of all the old references which the garbage collector cannot collect.

The solution is pretty simple, you can either define the class in its own file or you can make the class static. Static in this context means that the class is essentially treated like it were in it's own separate file and therefore cannot access the instance of the enclosing class.

So to summarise what you have to do is either keep the class nested and define it static like this:

public class YourActivity extends Activity {    private static class OnCreateListenerImpl implements OnCreateListener {        @Override        public void onObjCreate() {            Log.d("pltk", "dfgbfdgh");        }    }    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_palatok);        OnCreateListener inter = new OnCreateListenerImpl();        Intent in = new Intent(Palatok.this, SecondActivity.class);        in.putExtra("ob", inter);        startActivity(in);    }}

Or you can move the implementation in its own separate file:

public class OnCreateListenerImpl implements OnCreateListener {    @Override    public void onObjCreate() {        Log.d("pltk", "dfgbfdgh");    }}

Although the reason why you would want to send an OnCreateListener to another Activity still eludes me, this should solve your problem.

I hope I could help and if you have any further questions feel free to ask!


It is possible but such kind of Activity <-> Activity interraction will cause memory leaks. You should use LocalBroadcastManager (http://developer.android.com/reference/android/support/v4/content/LocalBroadcastManager.html) instead.