Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send a variable between classes through the Intent

I'm getting problems using the Intent for navigate through the screens. I want to send a variable and use it in the other class.

I'm using a method, the method takes the variable but i don't know how to send it with the intent to the new screen, which will use it to do some things.

Main class calls the metod:

private void pantallaDetalles(final int identificador)
{
     startActivityForResult(new Intent(this,MostrarDetalles.class),REQST_CODE);
}

MostrarDetalles.class is the *.java which will take the variable. I'm begining it like this:

public class MostrarDetalles extends Activity {

    SQLiteDatabase db;

    public void onCreate(Bundle savedInstanceState)
        {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.detalles);


         //more code...


         Cursor c = db.rawQuery("SELECT * FROM table WHERE _id="+ identificador, null);

        }

Did you see? I'm talking about this. I don't know how to send the "identificador" variable from the main class to the second class through the Intent.

Can you help me with this? Thank you very much in advice.

JMasia.

like image 271
JMasia Avatar asked Mar 17 '11 18:03

JMasia


People also ask

How do you pass data between activities using intent?

This example demonstrates how do I pass data between activities in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

What is the putExtra () method used with intent for?

Using putExtra() We can start adding data into the Intent object, we use the method defined in the Intent class putExtra() or putExtras() to store certain data as a key value pair or Bundle data object. These key-value pairs are known as Extras in the sense we are talking about Intents.

How do you send data to multiple activities?

Intent i = new Intent(this, ActivityB. class); i. putExtra("DATA1", "Hello"); i. putExtra("DATA2", "World"); startActivity(i);

How can I transfer data from one class to another in Android?

Many of our application reqiures sending data from one intent to another. This is done by putting data into one intent using putExtras() and getting it in the other intent using the getExtras() which matches the string value.


1 Answers

Use the extras bundle in the intent.

Intent i = new Intent(...);
i.putExtra("name_of_extra", myObject);

Then on onCreate:

getIntent.getIntExtra("name_of_extra", -1);
like image 98
Cheryl Simon Avatar answered Nov 14 '22 21:11

Cheryl Simon