我可以传递捆绑数据然后销毁 Activity 吗?

发布于 2024-12-10 20:03:37 字数 585 浏览 4 评论 0原文

如果我尝试将数据从一个活动传递到另一个活动,但在进入另一个活动之前,我销毁了初始活动,那么我将无法传递数据。查看代码

            //set bundle to pass data from initial activity
            bundle = new Bundle();
            data1 = Double.valueOf(myEditText.getText().toString());
            bundle.putDouble("data1", data1);

            Intent intent = new Intent(this, AnotherActivity.class);
            intent.putExtras(bundle);
            startActivity(intent);
            finish();

当我现在尝试通过 AnotherActivity 获取数据时,

myBundle = getIntent().getExtras();

我什么也得不到。

If I try to pass data from one activity to another, but before going to another activity, I destroy the initial activity, then I cannot pass data. Look at the code

            //set bundle to pass data from initial activity
            bundle = new Bundle();
            data1 = Double.valueOf(myEditText.getText().toString());
            bundle.putDouble("data1", data1);

            Intent intent = new Intent(this, AnotherActivity.class);
            intent.putExtras(bundle);
            startActivity(intent);
            finish();

When I now try to get data in AnotherActivity via

myBundle = getIntent().getExtras();

I get nothing.

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

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

发布评论

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

评论(3

森林散布 2024-12-17 20:03:37

我已经多次使用这种类型的代码,并且运行良好。
您需要检查 data1 = Double.valueOf(myEditText.getText().toString());
date1 是否为空?
需要在下一个活动中检查检索方法。
其他就没问题了。

I have used this type of code several time.and it's run fine.
You need to check out data1 = Double.valueOf(myEditText.getText().toString());
Is date1 is null?
Need to check retrieve method in next Activity.
other then no problem.

感情旳空白 2024-12-17 20:03:37

在这种情况下,bundle 和实例变量是吗?如果是这样,那可能就是问题所在。尝试为您要发送的特定意图创建一个新的捆绑包实例。

Is bundle in this case and instance variable? If so, that might be the problem. Try just creating a new bundle instance for this particular intent that you're going to send.

梦途 2024-12-17 20:03:37
Bundle b = new Bundle();
String value = "any data u want in another activity";
b.putStringArray("value", value);
Intent i = new Intent(CutrrentActivity.this, AnotherActivity.class);
i.putExtras(b);
startActivity(i);
finish();

// 在另一个活动中

Bundle b = this.getIntent().getExtras();
String value = b.getString("value");
Bundle b = new Bundle();
String value = "any data u want in another activity";
b.putStringArray("value", value);
Intent i = new Intent(CutrrentActivity.this, AnotherActivity.class);
i.putExtras(b);
startActivity(i);
finish();

// in another activity

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