通过捆绑传递 TIME 对象

发布于 2024-12-22 14:56:59 字数 122 浏览 2 评论 0原文

如何使用 Bundle 传递 TIME 的实例(对象)???

可能是一个简单的问题,但我需要一个准确的答案......

DATE date=new DATE();

How to pass an instance (object) of TIME using a Bundle???

May be a simple question,but i need a precise answer...

DATE date=new DATE();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

浅浅淡淡 2024-12-29 14:56:59

日期是可序列化的,因此您可以使用 get/putSerializes

MyFragment fragment = new MyFragment();
Bundle bundle = new Bundle();
bundle.putSerializable(MyFragment.DATE_KEY, new Date());
fragment.setArguments(bundle);

MyFragment 中:

public void onViewStateRestored(Bundle savedInstanceState) {
    super.onViewStateRestored(savedInstanceState);
    Bundle bundle = savedInstanceState != null ? savedInstanceState : getArguments();
    Date startTime = (Date) bundle.getSerializable(MyFragment.DATE_KEY);
    this.time = startTime;
}

public void onSaveInstanceState(Bundle bundle) {
    super.onSaveInstanceState(bundle);
    bundle.putSerializable(MyFragment.DATE_KEY, this.time);
}

Dates are serializable, so you can use get/putSerializable:

MyFragment fragment = new MyFragment();
Bundle bundle = new Bundle();
bundle.putSerializable(MyFragment.DATE_KEY, new Date());
fragment.setArguments(bundle);

In MyFragment:

public void onViewStateRestored(Bundle savedInstanceState) {
    super.onViewStateRestored(savedInstanceState);
    Bundle bundle = savedInstanceState != null ? savedInstanceState : getArguments();
    Date startTime = (Date) bundle.getSerializable(MyFragment.DATE_KEY);
    this.time = startTime;
}

public void onSaveInstanceState(Bundle bundle) {
    super.onSaveInstanceState(bundle);
    bundle.putSerializable(MyFragment.DATE_KEY, this.time);
}
臻嫒无言 2024-12-29 14:56:59

这段代码是近似的,因为我是凭记忆写的。

Intent mIntent = new Intent(ActivityA.this, ActivityB.class);
mIntent.putLong(KEY, getTimeMilliseconds());
startactivity(mIntent);

然后在ActivityB的onCreate中:

Bundle mBundle = getItent().getExtras();
Long time = mBundle.getLong(KEY);

注意:

putLong / getLong 可以适用于多种类型 String , int ...

如果您希望它应用于自定义对象,您应该创建该对象
实现 Parcelable。

This code is approximate since I am writing it from memory .

Intent mIntent = new Intent(ActivityA.this, ActivityB.class);
mIntent.putLong(KEY, getTimeMilliseconds());
startactivity(mIntent);

Then in the onCreate of ActivityB :

Bundle mBundle = getItent().getExtras();
Long time = mBundle.getLong(KEY);

Note :

putLong / getLong can apply to multiple type String , int ...

If you want it to apply to a custom object you should make that object
implement Parcelable.

扮仙女 2024-12-29 14:56:59

传递代表 Bundle 中日期的长值,例如 long time = new Date().getTime();

Pass the long value which represents your date in the Bundle e.g. long time = new Date().getTime();

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