Home > Google Android > Android using Bundle for sharing variables

Android using Bundle for sharing variables

Sharing variables between Activities is quite important point during development time of your Application. This  Example suppose Activity1 from where you run up other Activity2 based on your selection from submenu.   You gonna share variable myValue

From Activity1

Intent intent = new Intent(this,myActivity2.class);
Bundle bundle = new Bundle();
bundle.putString(“myValue“, myValue);
intent.putExtras(bundle);
navigation.this.startActivity(intent);

In Activity2

Bundle bundle = getIntent().getExtras();
act2MyValue= bundle.getString(“myValue“);

Now is your application powered to share variables between two different activities.

About these ads
Categories: Google Android
  1. Chris
    April 7, 2010 at 10:39 pm

    I’ve tried your example and it works when I execute the code from Activity 1 to pass the bundle to Activity 2.

    I’ve placed the code snippet in Activity 1 under a button, so it does not run with onCreate.

    However, I receive a force close error when I execute Activity 2 directly. I have a tab in Activity 1 opening Activity 2 (without the bundle code being executed). It fails at the line:
    Bundle bundle = getIntent().getExtras();

    Any ideas?

    • miragemiko
      April 12, 2010 at 12:10 pm

      Hi, sorry for late response I’ve been busy.
      have you tried also this case ?
      http://miragemiko.wordpress.com/2010/03/21/intent-put-variables-to-share/

      I’m not sure if I do understand well to your describtion … actually be aware when you run up new program you run up also new Dalvik instance.
      So I do undestand:: Activity 1 – contains button and you want to run Activity 2 and share variables. is that correct ?

      Have you registred your Activity 2 in the AndroidManifest.xml ? It’s one of common bug

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: