获取意图及其附加信息,但不是启动新活动的活动的意图
当您即将开始一个新活动并想要传递一个变量时,您通常只需执行以下操作:
Intent intent = new Intent().setClass(this, NewActivity.class);
intent.putExtra("variable", variable);
startActivity(intent);
当您正在阅读额外内容(在新活动中)时,您可以执行以下操作:
Intent intent = getIntent();
if(intent != null)
{
variable = intent.getIntArrayExtra("variable");
}
现在,在我的应用程序中,我有一个正在加载的内容所有加载发生的屏幕。这是在主要活动中进行的。在此过程中,许多变量正在更新/更改。我还有许多其他活动,我需要将这些新更新的变量传递给其中一些活动。问题是这些活动不是由我的主要活动启动。
有什么方法可以在我的主要活动中这样做:
Intent newIntent1 = new Intent().setClass(MainActivity.this, NewActivity1.class);
newIntent1.putExtra("var1", var1);
然后从另一个活动中使用 startActivity(newIntent1);
启动该活动?
我尝试像这样阅读额外内容:
Intent intent;
try
{
intent = Intent.parseUri("content://com.mycompany.android.MainActivity", 0);
if(intent != null)
{
var1 = intent.getIntArrayExtra("var1");
}
}
catch (URISyntaxException e)
{
e.printStackTrace();
}
这不起作用,var1
变量为空(如此初始化)。
老实说,我不知道如何做到这一点,也不知道 Intent.parseUri
是如何工作的。对于像我这样的初学者来说,这方面的文档确实很糟糕。
When you are about to start a new activity, and want to pass a variable, you usually just do this:
Intent intent = new Intent().setClass(this, NewActivity.class);
intent.putExtra("variable", variable);
startActivity(intent);
And when you are reading the extra (in the new activity), you do this:
Intent intent = getIntent();
if(intent != null)
{
variable = intent.getIntArrayExtra("variable");
}
Now, in my app I have a loading screen where all loading takes place. This is going on in the main activity. During this process, many variables are being updated/changed. I have many other activities, and I need to pass these newly updated variables to some of these activities. The problem is the fact that these activities are not started by my main activity.
Is there any way I can do it like this in my main activity:
Intent newIntent1 = new Intent().setClass(MainActivity.this, NewActivity1.class);
newIntent1.putExtra("var1", var1);
and then starting the activity using startActivity(newIntent1);
from another activity?
I have tried to read the extra like this:
Intent intent;
try
{
intent = Intent.parseUri("content://com.mycompany.android.MainActivity", 0);
if(intent != null)
{
var1 = intent.getIntArrayExtra("var1");
}
}
catch (URISyntaxException e)
{
e.printStackTrace();
}
This doesn't work, the var1
variable is null (initialized as so).
To be honest, I have no idea how to do this, or how Intent.parseUri
even works. The documentation for this is really bad for a beginner like me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,你不知道。
您需要编写一个真实的数据模型(数据库、内容提供程序、静态数据成员中的 POJO)并使所有活动都引用该通用数据模型。
No, you don't.
You need to write a real data model (database, content provider, POJOs in a static data member) and have all activities refer to that common data model.
仅使用广播来通知更改的活动会更容易。
It would be easier to just use a broadcast to inform the activities of changes.