0

I am porting a program to Android. I have all my business logic on POJOs, so I need to use Activities as a mere front-end.

The problem is that I don't know how to share POJO between Activities; I've tried with this, but it doesn't work:

class Activity1 extends Activity{
Logic logic=new Logic();

public Logic getLogic(){
return logic
}
}

class Activity2 extends Activity{
Logic logic;
public void onCreate(Bundle savedInstanceState) {
    main = (Activity1) findViewById((R.id.Activity1);
    logic= main.getLogic(); 
}
}

Please note that POJO is not for sharing data, it actually contains business logic.

michelemarcon
  • 23,277
  • 17
  • 52
  • 68

4 Answers4

6

Your POJOs need to implement the Parcelable interface. Then you can put them inside Intents using putExtra and retrieve them in the next activity using getParcelableExtra. http://developer.android.com/reference/android/os/Parcelable.html

Abhinav
  • 38,516
  • 9
  • 41
  • 49
  • 1
    They don't need to implement Parceable at all... you could just use putExtra and getExtra i did that a alot of times without any problems. – Chris Mar 11 '11 at 13:51
  • 1
    That would work for basic Java data types like int, long, double etc. For your own data types you would have to implement Parcelable or Serializable. – Abhinav Mar 11 '11 at 13:59
  • Oh I'm sorry you're right i just recognized that all my parsed Objects are Serializables :-). I did'nt see till now that this was a requirement. Thanks for illuminating +1 *gg* – Chris Mar 11 '11 at 14:08
  • Was confused for an instant myself. Had to go through the documentation just to check. :) – Abhinav Mar 11 '11 at 17:02
2

If you start another activity from one activity by issuing an Intent you can pass POJOs by using the method putExtra(). In the new activity where you receive the Intent you can then get the POJO back by using getXXXExtra() where XXX ist the POJOs Type.

You should also have a look at http://developer.android.com/guide/topics/intents/intents-filters.html for a better understanding what Intents are and how they work together with Activities.

edit: as stated in the other answers here you'll have to implement either Parceable or Serializable Interface.

Chris
  • 7,675
  • 8
  • 51
  • 101
2
main = (Activity1) findViewById((R.id.Activity1);

findViewById works only for views! Its not meant to be used for activities as an activity is more like a "screen" and not a view itself.

If it is possible for your business logic to be a singleton, than I would recommend to make it so. It should be the easiest way.

WarrenFaith
  • 57,492
  • 25
  • 134
  • 150
  • 1
    Please read the last two sentences. If you can make them a singleton, use it. Otherwise I suggest to extend the Application class and keep a reference to your POJOs there, so you can access them from everywhere... – WarrenFaith Mar 11 '11 at 11:42
1

I see that you are mixing two different things: findViewById will get you a View, not an Activity like what you tried to do.

If your logic doesn't have to keep the state between activities, you can simply create a new object in both activities

Logic logic=new Logic();

If you want to keep the state, assuming it's a POJO, you can send the data via the intent when you are "calling" the second activity

intent.putExtra("MyInt", 123);
intent.putExtra("MyString", "hello!");
//...

and then in the second activity

intent.getIntExtra("MyInt"); // 123
intent.getStringExtra("MyString"); //"hello!"

Another option is to implemente parcelable . You have a sample in that link.

Pedro Loureiro
  • 11,436
  • 2
  • 31
  • 37
  • I need to communicate both ways between activities and for a lot of variables this would become cumbersome... Binder is a better option. – michelemarcon Mar 11 '11 at 14:02