0

I'm trying to pass an Object from one activity to another and I know I should use Parcelable or Serializable but my Object class implements an interface already. Is there any way around this?

John
  • 1,808
  • 7
  • 28
  • 57

5 Answers5

2

Objects can implement multiple interfaces:

class MyClass implements Interface1, Parcelable {
  // Implement each interface
}
ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
  • Wow I knew that. Sorry, Thank you very much! I'll mark your answer as correct once I can. – John Jan 19 '16 at 22:17
2

I think the right thing is just to use Intent.putExtras() - where you can pass primitive data types + objects of type String, Bundle, Parcelable, Serializable. You are simply using key/value pairs. And after that you can get your data by Intent.getExtras(). Everything is quite simple. Also have a look at this links, they are for bigginers, but really helpful: http://developer.android.com/guide/components/intents-filters.html and http://www.vogella.com/tutorials/AndroidIntent/article.html. If the problem is somewhere deeper - please describe it. Thanks.

Yurii Tsap
  • 3,554
  • 3
  • 24
  • 33
1

Sure! A class can implement multiple interfaces. You'll just need to separate each one with a comma in your class declaration, like this...

public class YourClass implements interface1, interface2, interface3 { 
    //...
}
NoChinDeluxe
  • 3,446
  • 1
  • 16
  • 29
1

An object cannot extend more than one class but can implement many interfaces.

andrea.petreri
  • 4,137
  • 2
  • 22
  • 21
0
Parcelable and `Serializable` are the way but little complex. an easy solution is just use `Gson` or any other JSON library..

in first activity.

String objJson = new Gson().toJson(object);
intent.putExtra("key",objJson);

and in your second activity

YourClass yourClass = new Gson().fromJson(getIntent().getStringExtra("key"),YourClass.class) ;

for Gson library check this link http://www.java2s.com/Code/Jar/g/Downloadgson222jar.htm

Adnan Ali
  • 792
  • 1
  • 8
  • 21