android:如何在活动之间共享POJO [英] android: How to share a POJO between Activities
问题描述
我正在将程序移植到Android.我在POJO上拥有所有的业务逻辑,因此我需要将Activity用作前端.
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.
问题是我不知道如何在活动"之间共享POJO.我已经尝试过了,但是不起作用:
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();
}
}
请注意,POJO并非用于共享数据,它实际上包含业务逻辑.
Please note that POJO is not for sharing data, it actually contains business logic.
推荐答案
main = (Activity1) findViewById((R.id.Activity1);
findViewById
仅适用于视图!它不是要用于活动,因为活动更像是屏幕",而不是视图本身.
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.
这篇关于android:如何在活动之间共享POJO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!