10

I need to start an activity from 2 different screens that have two different models but, both models have some shared information which is the one that I need in the new activity. The problem is that I cannot make those models to extend from the same parent, as one of the models already extends one parent. I have thought about creating an interface that contains the shared methods but, if I do that, then how can I put that interface in the bundle required to start the next activity?

I add some simplified code in order to clarify my situation:

public class A extends Model implements CustomInterface {
    String name;
    String address;

    public String getName(){
        return name;
    }

    public String getAddress() {
        return address;
    }
}

public class B implements CustomInterface {
    String name;

    public String getName() {
        return name;
    }
}

public interface CustomInterface {
    String getName();
}

My problem is that I need to start an activity with a bundle with the shared information between both models. So, I would like to put CustomInterface in a bundle. How could I do that?

Thanks in advance.

FVod
  • 1,955
  • 3
  • 17
  • 41
  • 1
    I do NOT think there is a need for putting the interface in a bundle. When the activities that start the second activity have the data, you need to send the data and NOT the interface. The interface is a medium to allow you to tell that your class will conform to a certain specification when it implements the interface. – Narayan Acharya Jan 13 '16 at 19:10
  • 1
    I think you need to rethink about your application architecture as I see there's a serious problem in there. Anyway, if you need to pass the information in a bundle towards another activity you can put them easily before starting the activity. From the launched activity's constructor check if the bundle exists and then define your actions accordingly. There's another way around for storing data and fetching over activities. Declare a public static variable and then store the data which will be carried throughout the application's life-cycle. Hope that helps. – Reaz Murshed Jan 13 '16 at 19:16

2 Answers2

17

So, I would like to put CustomInterface in a bundle

you could let CustomInterface extend Parcelable. E.g.

 public interface CustomInterface extends Parcelable {
     String getName();
 }

this way the classes implementing CustomInterface will have to implements the method defined in the Parcelable interface. If implemented correctly, you will be able to pass those objects around without problems

Blackbelt
  • 148,780
  • 26
  • 271
  • 285
  • 1
    Exactly, and when you retrieve the Parcelable I your Activity, you can cast it to your interface. – Desdroid Jan 13 '16 at 19:47
  • Not sure why but in my case, using service, I received a **W/ActivityManager: Timeout executing service** warning followed by a **E/ActivityManager: ANR in site.xplor.player** error because `.getParcelableExtra( KEY )` runs with timeout – HKoncept Dec 28 '17 at 14:29
0

Create a singleton class, then you can share data without passing it:

public class MySingleton
{
    private static MySingleton instance;

    public String customVar;

    public static MySingleton getInstance()
    {
        if (instance == null)
        {
            // Create the instance
            instance = new MySingleton();
        }
        // Return the instance
        return instance;
    }

    private MySingleton()
    {
        // Constructor hidden because this is a singleton
    }

    public void getSomeData()
    {
        return something;
    }

    public void getSomeOtherData()
    {
        return somethingelse;
    }
}

Then in your classes:

public class A extends Model {
    String name;
    String address;

    public String getName(){
        return name;
    }

    public String getAddress() {
        return address;
    }

    public String doSomethingWithSharedData(){
         MySingleton model = MySingleton.getInstance();
         String somedata = model.getSomeData();
    }
}

public class B {
    String name;


    public String getName() {
        return name;
    }

    public String doSomethingDifferentWithSharedData(){
         MySingleton model = MySingleton.getInstance();
         String somedata = model.getSomeOtherData();
    }
}
markt
  • 895
  • 7
  • 20
  • Keeping static data should be avoided unless needed across the entire process life. For passing data between activities the api android provides is recommended. – Goran Horia Mihail Feb 11 '18 at 06:03