Solo
Solo

Reputation: 4126

How to open a new screen with additional parameters?

I've read through the FAQ of Android Dev Guid (http://developer.android.com/guide/appendix/faq/commontasks.html#opennewscreen) but I'm not sure how can I open a new screen with additional parameters passed into it.

Let's say I'm going to open screen2, with a variable indicating the current user name so that I could greet the users. Is it possible?

Intent i; 
i = new Intent(this, screen2.class);  
//How to pass variable to screen2?
startActivity(i); 

Upvotes: 2

Views: 634

Answers (3)

lbedogni
lbedogni

Reputation: 7997

Use the putExtra method. Something like:

Intent i = new Intent(this, myNew.class);
i.putExtra("key",keyValue);

and on the other side just the getter:

this.getIntent().getIntExtra("key"); // If keyvalue was an int

Upvotes: 2

Pentium10
Pentium10

Reputation: 207838

Start Intent by using:

Intent foo = new Intent(this, viewContactQuick.class);
        foo.putExtra("id", id);
        this.startActivity(foo);    

and in onCreate event you can get the id

id = this.getIntent().getLongExtra("id", 0);

Upvotes: 4

ccheneson
ccheneson

Reputation: 49410

From the link you have, lookup for putExtra.

The putExtra method (from an Intent object) is used to pass data between activities.

To send data to another activity, use putExtra.

To receive data from another activity, use getExtra

A short description to read

Upvotes: 0

Related Questions