android:如何在 Activity 之间共享 POJO

发布于 2024-10-21 04:00:28 字数 465 浏览 2 评论 0原文

我正在将程序移植到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

薄荷港 2024-10-28 04:00:28

您的 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

沒落の蓅哖 2024-10-28 04:00:28
main = (Activity1) findViewById((R.id.Activity1);

findViewById 仅适用于视图!它并不意味着用于活动,因为活动更像是“屏幕”而不是视图本身。

如果您的业务逻辑有可能是单例,那么我建议这样做。这应该是最简单的方法。

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.

最初的梦 2024-10-28 04:00:28

如果您通过发出 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 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.

生活了然无味 2024-10-28 04:00:28

我发现您正在混合两种不同的东西: findViewById 将为您提供一个 View,而不是像您尝试做的那样的 Activity

如果您的逻辑不必保留活动之间的状态,则只需在两个活动中创建一个新对象

Logic logic=new Logic();

如果您想保留状态,假设它是 POJO,您可以在“调用”第二个活动时通过 Intent 发送数据

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

,然后在第二个活动中

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

另一个选择是实现 parcelable 。您在该链接中有一个示例。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文