4

I have some variables that I want to pass to my next activity, but I can't figure a way to do that.

My variables are:

JSONObject jsonObj = jsonArray.getJSONObject(i);
String propId = jsonObj.getString("id");
Log.i("Value id", propId);
String propCity = jsonObj.getString("city");
Log.i("Value city", propCity);
String propPlace = jsonObj.getString("place");
Log.i("Value place", propPlace);
String propStation = jsonObj.getString("station");
Log.i("Value station", propStation);

and the code I used to get them is:

Bundle extras = new Bundle();
extras.putString("id", propId);
extras.putString("city", propCity);
extras.putString("place", propPlace);
extras.putString("station", propStation);

Can anyone help me with this please ?

ThatBlairGuy
  • 2,426
  • 1
  • 19
  • 33
user3507621
  • 61
  • 1
  • 1
  • 6
  • 1
    Possible duplicate of [How do I pass data between Activities in Android application?](http://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-in-android-application) – Md Imran Choudhury May 08 '17 at 02:59

5 Answers5

6

The code you posted is to write them to the Bundle, after you write in the Bundle use .putExtras() to put your bundle in your Intent.

intent.putExtras(bundle);

Example:

Intent intent = new Intent(this, YourClass.class);
Bundle extras = new Bundle();
extras.putString("id", propId);
extras.putString("city", propCity);
extras.putString("place", propPlace);
extras.putString("station", propStation);
intent.putExtras(bundle);
startActivity(intent);

To read it in your activity use getExtras() to get the Bundle you passed to it and then use getString/getXXX.

Anyway, you can avoid the creation of the Bundle and use directly the set methods of Intent which works in the same way.

So it would be:

Intent intent = new Intent(this, YourClass.class);
intent.putExtra("id", propId);
intent.putExtra("city", propCity);
intent.putExtra("place", propPlace);
intent.putExtra("station", propStation);
startActivity(intent);
Marco Acierno
  • 14,682
  • 8
  • 43
  • 53
  • I did it, but the problem is that Eclipse tells me that propId, propCity, propPlace and propStation can't be resolved to variables. – user3507621 Apr 07 '14 at 16:53
  • You should add more code, this problem could be caused by the fact you read there prop in a for and then try to read it out the for – Marco Acierno Apr 07 '14 at 16:54
  • That's it, I set them in a for, and my putExtra is out of the for. – user3507621 Apr 07 '14 at 16:56
  • propId/etc. lives only inside the for or you declare them out of the for or you put the putExtras code in the for – Marco Acierno Apr 07 '14 at 16:56
  • Okay, I have no more errors on the first activity. Now, I need to assign my values to differents TextView or something like that, can you give me an hint ? – user3507621 Apr 07 '14 at 17:04
  • I have this code: Bundle extras = getIntent().getExtras(); String dateDepl = extras.getString("id"); String cityDepl = extras.getString("city"); String placeDepl = extras.getString("place"); String stationDepl = extras.getString("station"); – user3507621 Apr 07 '14 at 17:04
  • Yes, then to print it to a TextView just do textView.setText(dateDepl); remember in your variables you will have what you passed (and check if getExtras is not null too!) – Marco Acierno Apr 07 '14 at 17:05
  • How can I assign it by the id of the textView ? For example, with tvDate which will receive dateDepl ? – user3507621 Apr 07 '14 at 17:12
  • I don't understand your comment? The textview gets the id by findViewById... open a new question if you got a new question you will get more answers – Marco Acierno Apr 07 '14 at 17:34
  • I finished it, but I got a problem. Since the extras.putString are in the for, they always take the value of the lasts propId, propCity, etc... But I want it to take the values of the item I click on. Do you got any hints for it please ? – user3507621 Apr 07 '14 at 17:39
  • Yes, you should choose what you want to do... send all info or the last one? the first? what? – Marco Acierno Apr 07 '14 at 17:40
  • I want to send the info for the item I click on – user3507621 Apr 07 '14 at 17:41
  • then you need to pass jsonArray.getJSONObject(itemClicked) without code i'm unable to help you more. But anyway, accept this answer and post a new question with the code (as i said, you will get more answer and a more clear question). – Marco Acierno Apr 07 '14 at 17:43
2

Use Parcelables instead. Here you have an example.

Parcelable is the way Android uses to communicate between activites. In order to use it, you must create a class that implements Parcelable:

import android.

public class MyClass implements Parcelable
{
     private string userName;
     private int userAge;

     public void SetUserName(string name) { userName = name; }
     public string GetUserName() { return userName; }

     public void SetUserAge(int age) { userAge = age; }
     public int GetUserAge() { return age; }

    // Some inner stuff
}

This class needs to implement the WriteToParcel method, which writes into a Parcel all the needed info:

    @Override 
    public void WriteToParcel(Parcel dest, int flags)
    {
         dest.writeString(userName);
         dest.writeInt(userAge);
         // Since here you wrote into dest Parcel your string and int
     }

In order to be able to read from that Parcel, you need to do the following:

public static final Parcelable.Creator<ExtractionConfig> CREATOR = new Creator<ExtractionConfig>()         {
{
    @Override
    public MyClass [] newArray(int size) {
        return new MyClass [size];
    }

    @Override
    public MyClass createFromParcel(Parcel source) {
        MyClass toReturn = new MyClass ();
        toReturn.setUserName(source.readString());
        toReturn.setUserAge(source.readInt());
        return toReturn;
    }
};

IMPORTANT: the writeToParcel order MUST BE THE SAME than the createFromParcel order!!

How to pass Parcel between activities:

When you launch an activity from another one, you call an Intent. You can put extra information in it by creating an object of the Parcelable class and fill it:

MyClass myclass = new MyClass();
myclass.setUserName("first user");
myclass.setUserAge(20);

And then put that extra information into your Intent:

In your main activity:

Intent intent = new Intent(this, ActivityToBeCalled.class);
intent.putExtra(myclass, "extraclass"); // look at this string, this one will be your identifier to receive the extra information
startActivity(intent);

To read information from the intent in the called class:

In your second activity:

Intent intent = getIntent();
MyClass myReceivedClass = intent.getParcelableExtra("extraclass"); // here you use your string identifier defined in putExtra(...);

And now, in myReceivedClass you should have all your MyClass information.

Community
  • 1
  • 1
Sonhja
  • 8,230
  • 20
  • 73
  • 131
1

You doesn't need to use Bundle. You can use putExtra() of Intent class to pass data to next Activity as follows...

Intent intent = new Intent(CurrentActivity.this, NextActivity.class);

intent.putExtra("id", propId);
intent.putExtra("city", propCity);
intent.putExtra("place", propPlace);
intent.putExtra("place", propPlace);

startActivity(intent);
Hamid Shatu
  • 9,664
  • 4
  • 30
  • 41
0

Use intents.

Intent intent = new Intent(context,target_activity.class);  
intent.putExtra("name",bundle);  

startActivity(intent);  

For small and typical data its good enough.For sending objects,use parcelable.

Hamid Shatu
  • 9,664
  • 4
  • 30
  • 41
Saikat Das
  • 95
  • 1
  • 7
0

No idea how it was handled earlier, but nowadays you can use intent.putExtra("extra id", obj) where obj is any Serializable implementing object. Using intent.getSerializableExtra("extra id") you gain an object you can cast to your initially used class.

Hubert Grzeskowiak
  • 15,137
  • 5
  • 57
  • 74