Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass interface between activities in intent - interface fails to be Serializable or Parcelable

I want to pass an interface from 1st activity to 2nd activity.

I want to initiate methods from the interface from the 2nd activity which will affect the 1st activity.

I'm well aware that it's very overkilling not using the onActivityResult mechanism, and that it might not be good programming, but roll with me please.

Here's the issue - my interface can't implement Serializable / Parcelable since interface can't implement another class.

This is my interface :

public interface ITest {
     void onSuccess(String text);
}

But, i can't start my activity with this interface since it's not Parcelable.

intent.putExtra("testInterface", new ITest() {
    @Override 
    void onSuccess(String text) {
    }
}

Obviously, i receive a compilation error : Cannot resolve method 'putExtra(java.lang.String, ITest)'

like image 872
Dus Avatar asked Aug 11 '16 09:08

Dus


People also ask

When to use Parcelable in Android?

Parcelable and Bundle objects are intended to be used across process boundaries such as with IPC/Binder transactions, between activities with intents, and to store transient state across configuration changes.

What are some differences between Parcelable and serializable?

Parcel able is faster than serializable. Parcel able is going to convert object to byte stream and pass the data between two activities. Writing parcel able code is little bit complex compare to serialization. It doesn't create more temp objects while passing the data between two activities.

How do I pass model data from one activity to another in Android?

We can send the data using putExtra() method from one activity and get the data from the second activity using the getStringExtra() method.

How pass data from activity to class in Java Android?

String value = A. _utfValue ; Option 3: You can use SharedPreference to save the value and get it from other class. Option 4: You can use a static method with some return value and fetch the method in your java class through class name.


1 Answers

You cannot "pass an interface". An "interface" is an abstract thing. What you want to pass is a concrete implementation of that interface, ie: an object (that happens to implement your interface). When you instantiate your "interface" (in your example, like this:

intent.putExtra("testInterface", new ITest() {
    @Override 
    void onSuccess(String text) {
    }
}

you are actually creating an instance of an anonymous class that implements the interface ITest. To pass this in an Intent you would need to make this class also implement Parcelable or Serializable.

However, even if you did that, it would not solve your problem. What you need to understand is that you can't pass objects (instances) by putting them as "extras" in an Intent. When you do that, Android actually serializes and then deserializes the object so that you end up with 2 objects, one is a serialized/deserialized copy of the original.

If you want ActivityB to communicate with ActivityA, you will need to use another technique. Try one of these:

  • ActivityB sends a broadcast Intent, which ActivityA listens for
  • ActivityA starts ActivityB using startActivityForResult() and ActivityB sends data back to ActivityA using setResult()
  • Use public static (ie: global) variables to communicate
  • Store data in shared preferences, or a file, or a database

What you really need to understand is that, under certain conditions, the following can occur:

  • your app is running with ActivityA in the stack and ActivityB on the top of the stack
  • user presses HOME
  • Android moves your task to the background and eventually kills the hosting process
  • User returns to your app (by starting it again or selecting it from "recent task" list
  • Android creates a new process for your app, and instantiates ActivityB, then calls onCreate(), onStart() and onResume() of ActivityB.

In this case, there is no instance of ActivityA anymore. That instance is dead. So ActivityB cannot communicate with ActivityA because it no longer exists.

like image 152
David Wasser Avatar answered Nov 15 '22 18:11

David Wasser