6

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.

Robby Pond
  • 73,164
  • 16
  • 126
  • 119
JMasia
  • 103
  • 1
  • 1
  • 6
  • Almost dupe of http://stackoverflow.com/questions/2139134/how-to-send-an-object-from-one-android-activity-to-another-with-intent-putextra? – Andrew White Mar 17 '11 at 18:33

3 Answers3

13

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);
Cheryl Simon
  • 46,552
  • 15
  • 93
  • 82
  • This is correct. Your variable will have to implement Parcelable if you want to put absolutely anything into the intent. – eternalmatt Mar 17 '11 at 18:39
  • If i'm sendin an int variable: `private void pantallaDetalles(final int identificador) { Intent i; startActivityForResult(i = new Intent(this,MostrarDetalles.class),REQST_CODE); i.putExtra("identificador", identificador); }` how do i read the variable in the second class? I used the getIntegerExtra, but it dosn't work. – JMasia Mar 17 '11 at 19:14
  • @eternalmatt you can also send primitive or serializable types. – Cheryl Simon Mar 17 '11 at 19:52
  • @JMasia Its actually getIntExtra, updated. See the docs: http://developer.android.com/reference/android/content/Intent.html#getIntExtra(java.lang.String, int) – Cheryl Simon Mar 17 '11 at 19:54
  • @Mayra. I really have to thank you all the help that you are giving to me, but i would be doing something wrong. The app always shows the default value. What do you think about it? – JMasia Mar 17 '11 at 20:26
  • Its hard to tell for the code snippet,but it looks like you are adding the extra after you are calling startActivityForResult? That doesn't make any sense. You need to finish configuring the intent before you send it. – Cheryl Simon Mar 17 '11 at 21:14
  • Yeah, that was just the problem. What a silly mistake! Thank you very much, youre awesome. :-) – JMasia Mar 17 '11 at 21:21
6

Screen 1:

Intent i=new Intent("com.suatatan.app.Result");
                i.putExtra("VAR_RESULT", "Added !");
                startActivity(i);

Screen 2: (Receiver):

TextView tv_sonuc = (TextView) findViewById(R.id.tv_sonuc);

Bundle bundle = getIntent().getExtras();

String var_from_prev_intent = bundle.getString("VAR_RESULT");
tv_sonuc.setText(var_from_prev_intent);
Suat Atan PhD
  • 1,152
  • 13
  • 27
0

You can use Intent.putExtra() to bundle the data you want to send with the intent.

Franci Penov
  • 74,861
  • 18
  • 132
  • 169