Android: How to send interface from one activity to another

I have an interface like this:

public interface MyInterface {
    public void aMethod();
}

My custom Object:

  • Android on Drawer Closed Listener
  • How can I draw seamless curve using android graphics ?
  • Android Studio : eglSurfaceAttrib not implemented
  • Getting the Application Context
  • Swipe and OnClick events in RecyclerView
  • Why “This app has been built with an incorrect configuration” error occured in some phones?
  • public class MyObject {
    
        private Context context;
        private MyInterface inter;
    
        public MyObject(Context context) {
            this.context = context;
            this.inter = (MyInterface) this.context;
            inter.aMethod();
        }
    }
    

    MainActivity:

    public class MainActivity extends Activity implements MyInterface {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            MyObject my = new MyObject(this);
    
        }
    
        @Override
        public void aMethod() {
            Toast.makeText(this, "done", Toast.LENGTH_SHORT).show();
        }
    }  
    

    Here, inside MyObject constructor i can get the interface from the context and then communicate with the Activity.

    But how can i send interface from one activity to another ?
    I need to call a method inside Activity1 from Activity2
    Is there a way like this ?

    Note: i don’t want to use fragment.

  • Android VideoView not playing Portrait Orientation
  • Generate iPhone and iPad screenshots including device frame
  • How to use default notification style?
  • Setting TextView TextAppeareance Programmatically in android
  • Android Studio - with Junit 4.12 “!!! JUnit version 3.8 or later expected:”
  • How Do I Set Up Visual Studio 2015 with SDL2 For Android, iOS, and Windows Development?
  • 2 Solutions collect form web for “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:

    @Override
    protected 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.

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.