3

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.

gosr
  • 4,593
  • 9
  • 46
  • 82
  • where in the intent that you are starting are you trying to read the values out of the intent? onCreate? onNewIntent? – Brandon Haugen Aug 07 '11 at 15:26
  • @Brandon Haugen: I'm trying to read it in the onCreate method. – gosr Aug 07 '11 at 15:31
  • you say that the activities are not started by your main activity, so who starts them? The point is that if this is not the first time the activity is being started try reading the values in the onNewIntent method of the activity instead of onCreate – Brandon Haugen Aug 07 '11 at 15:35

2 Answers2

1

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Yeah it seems like it, will move on with a database. – gosr Aug 07 '11 at 21:20
  • To others that might have the same problem as I had initially, I can safely say that using a database is much easier. – gosr Aug 09 '11 at 16:10
0

It would be easier to just use a broadcast to inform the activities of changes.

Earl
  • 791
  • 4
  • 9
  • I've never heard of broadcast before, can you please make an example? Checked the documentation for it, but not quite sure how to implement it. – gosr Aug 07 '11 at 15:49
  • I've already looked at that site, but those examples are good if you're going to use the phone state or similar. I just need my own activities, and I can't find an example in there that shows this. – gosr Aug 07 '11 at 16:09
  • In the broadcast receiver example, remove "android.intent.action.PHONE_STATE" from the intent filter and replace it with a name unique to your app. To send a broadcast to receivers use `Intent intent = new Intent("my.unique.string"); sendBroadcast(intent);` To send more information to the receivers, before sendBroadcast(), you can `intent.putExtra("key", value);` with any number of key/value pairs. – Earl Aug 07 '11 at 16:23
  • Thanks, now it's possible for me to reach the onReceive method in the receiver. Now, how do I read the information from the `extra` in my new activity? – gosr Aug 07 '11 at 16:32
  • The receiver gets the intent. Check the documentation for the Intent class. There are a bunch of get methods to get the extras. For example to get a string extra, you would call `intent.getStringExtra("key")` – Earl Aug 07 '11 at 16:37
  • Yeah, but I'm not interested in reading the extra in the onReceive method (or even in the Broadcast class). For instance, in another class I need to start the activity, to which I have added some extras. I would do this: `Intent newIntent1 = new Intent("com.myapplication.android.NewActivity1");` followed by `startActivity(newIntent1);`. And then, in this new activity, I would read the extra as shown at the top of my original post. But this doesn't work.. – gosr Aug 07 '11 at 16:43
  • And I'm strongly suggesting to you that you need to rethink your approach. The way to get information to something to which you do not direct access is a message. The discussion is already too long... good luck. – Earl Aug 07 '11 at 16:55