简体   繁体   中英

Getting intent and its extras, but not the intent of the activity that started the new activity

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.

I have many other activities, and I need to pass these newly updated variables to some of these activities.

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.

使用广播来更新活动会更容易。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM