android:如何在 Activity 之间共享 POJO
我正在将程序移植到 Android。我的所有业务逻辑都在 POJO 上,因此我需要使用“活动”作为纯粹的前端。
问题是我不知道如何在Activity之间共享POJO;我已经尝试过这个,但它不起作用:
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 不是用于共享数据,它实际上包含业务逻辑。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的 POJO 需要实现 Parcelable 接口。然后,您可以使用 putExtra 将它们放入 Intents 中,并使用 getParcelableExtra 在下一个活动中检索它们。 http://developer.android.com/reference/android/os/Parcelable.html
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
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.
如果您通过发出 Intent 从一个 Activity 启动另一个 Activity,您可以通过使用
putExtra()
方法来创建 POJO。在接收 Intent 的新活动中,您可以使用 getXXXExtra() 取回 POJO,其中 XXX 是 POJO 类型。您还应该看看 http://developer.android.com/ guide/topics/intents/intents-filters.html,以便更好地理解意图是什么以及它们如何与活动一起工作。
编辑:正如这里的其他答案所述,您必须实现 Parceable 或 Serializing 接口。
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 usinggetXXXExtra()
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.
我发现您正在混合两种不同的东西:
findViewById
将为您提供一个View
,而不是像您尝试做的那样的Activity
。如果您的逻辑不必保留活动之间的状态,则只需在两个活动中创建一个新对象
如果您想保留状态,假设它是 POJO,您可以在“调用”第二个活动时通过 Intent 发送数据
,然后在第二个活动中
另一个选择是实现 parcelable 。您在该链接中有一个示例。
I see that you are mixing two different things:
findViewById
will get you aView
, not anActivity
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
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
and then in the second activity
Another option is to implemente parcelable . You have a sample in that link.