× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

Android: How to send interface from one activity to another

Xaver Kapeller
Mar 13, 2015
<p>First and foremost, this is very bad:</p> <pre><code>this.inter = (MyInterface) this.context; </code></pre> <p>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:</p> <pre><code>public class MyObject { private Context context; private MyInterface inter; public MyObject(Context context, MyInterface inter) { this.context = context; this.inter = inter; inter.aMethod(); } } </code></pre> <p>This way it's much safer, and cleaner. </p> <hr> <p>To send the <code>Object</code> to another <code>Activity</code> make sure it implements <code>Serializable</code> like this:</p> <pre><code>public interface MyInterface extends Serializable { public void aMethod(); } </code></pre> <p>And now you can just add the interface as an extra to the <code>Intent</code> which starts the other <code>Activity</code>:</p> <pre><code>Intent intent = new Intent(context, OtherActivity.class); intent.putExtra("interface", inter); startActivity(intent); </code></pre> <p>And in the <code>OtherActivity</code> you can get the <code>Object</code> from the <code>Intent</code> like this:</p> <pre><code>@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Intent intent = getIntent(); MyInterface inter = (MyInterface) intent.getSerializableExtra("interface"); ... } </code></pre> <hr> <h1>EDIT:</h1> <p>When you do something like this in your <code>Activity</code> you are creating an anonymous class:</p> <pre><code>OnCreateListener inter = new OnCreateListener() { @Override public void onObjCreate() { Log.d("pltk", "dfgbfdgh"); } }; </code></pre> <p>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 <code>Activity</code>. This is a great thing and we use it all the time for <code>OnClickListener</code> etc. But in your case it is a problem because you want to send this <code>Object</code> to another <code>Activity</code>. The internal reference to the old <code>Activity</code> keeps it from being serialised and that is a good thing. If you could just send an <code>Object</code> like that you would create memory leaks like crazy because of all the old references which the garbage collector cannot collect. </p> <p>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.</p> <p>So to summarise what you have to do is either keep the class nested and define it static like this:</p> <pre><code>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); } } </code></pre> <p>Or you can move the implementation in its own separate file:</p> <pre><code>public class OnCreateListenerImpl implements OnCreateListener { @Override public void onObjCreate() { Log.d("pltk", "dfgbfdgh"); } } </code></pre> <p>Although the reason why you would want to send an <code>OnCreateListener</code> to another <code>Activity</code> still eludes me, this should solve your problem.</p> <p>I hope I could help and if you have any further questions feel free to ask!</p> <p>This tip was originally posted on <a href="http://stackoverflow.com/questions/24780000/Android:%20How%20to%20send%20interface%20from%20one%20activity%20to%20another/24780184">Stack Overflow</a>.</p>

Get New Tutorials Delivered to Your Inbox

New tutorials will be sent to your Inbox once a week.

comments powered by Disqus