0

I need to pass the values from screen1 to screen4. I declared the values as static but in that way the values are not passing. After screen1 I need to call screen2 and screen3. In screen1 button click I am getting the values but how to send them to screen4. Any suggestion in this regard will be thankful.

user1448108
  • 467
  • 2
  • 10
  • 28

5 Answers5

1

You should pass your values along in the EXTRAS of the intent that you're using to launch the new Activity. You can use one of the many putExtra functions in the Intent class.

Kurtis Nusbaum
  • 30,445
  • 13
  • 78
  • 102
  • hi thanks for response. I dont want those values in screen2 and screen3..I directly want to send those values to screen4. – user1448108 Dec 01 '12 at 07:20
  • So you pass them to screen2, then pass them from screen2 to screen 3, then pass them from screen3 to screen 4 – Kurtis Nusbaum Dec 01 '12 at 07:21
  • ok but if there are 10 screens in the middle then also the same we need to do? or do we have any alternate method for this? – user1448108 Dec 01 '12 at 07:30
  • Just keep passing along the value. You may also wanna make sure you're approaching the problem correctly, i.e. since you don't need this value until later in the program, why not just obtain the value on screen 4 instead of screen 1? – Kurtis Nusbaum Dec 01 '12 at 07:32
  • 1
    instead use shared preference once you store you can access it anywhere you want .try it – Ghouse Dec 01 '12 at 07:32
1

you declare the static variable out side of your activity. means in a separate class. and try to access them. it will work.

kumar
  • 691
  • 1
  • 7
  • 16
1

you can create standalone class like this:

public class Myclass{
    private String myString;
    public void setString(String str){
    myString=str;
    }
    public String getString(){
    return myString;
    }
}

and in your Activity1 you can assign value to the string by

String myString="What ever the string you have"
MyClass.setString(myString);

and you can call that string in any place you want by this:

String myString=MyClass.getString():
Husam A. Al-ahmadi
  • 2,056
  • 2
  • 20
  • 27
1

Try to avoid using static as you can unknowingly cause memory leaks and if you must have to use static then make sure that you are not holding context of your activity (directly/indirectly).

You can also use Parcelable especially when your data is complex and large. Create a POJO for your data and implement parcelable

public class Student implements Parcelable{
    private String id;
    private String name;
    private String grade;

    // Constructor
    public Student(String id, String name, String grade){
        this.id = id;
        this.name = name;
       this.grade = grade;
   }
   // Getter and setter methods
   .........

   // Parcelling part
   public Student(Parcel in){
       String[] data = new String[3];

       in.readStringArray(data);
       this.id = data[0];
       this.name = data[1];
       this.grade = data[2];
   }

   @override
   public int describeContents(){
       return 0;
   }

   @Override
   public void writeToParcel(Parcel dest, int flags) {
       dest.writeStringArray(new String[] {this.id,
                                           this.name,
                                           this.grade});
   }
   public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
       public Student createFromParcel(Parcel in) {
           return new Student(in); 
       }

       public Student[] newArray(int size) {
           return new Student[size];
       }
   };

}

Once you make your POJO parcelable, you can pass objects of this class through the Intent.

intent.putExtra("student", new Student("2","Ryan","9"));

Then you can recover this object in the target activity

Bundle data = getIntent().getExtras();
Student student = data.getParcelable("student");
Atul Kaushik
  • 5,181
  • 3
  • 29
  • 36
0

The easiest way to do this would be to pass the value in the intent you're using to start the activity:

Intent intent = new Intent(getBaseContext(), Your4NumberScreen.class);
intent.putExtra("new_variable_name", "valueToPass");
startActivity(intent)

Then in the new Activity, retrieve those values:

Bundle extras = getIntent().getExtras();
if (extras != null) {
    String value = extras.getString("new_variable_name");
}

Edit : If you want to directly pass the value to screen 4 then you can use otherwise you have to pass to screen2 then from screen2 to screen3 then from 3 to 4 using above code.

Chirag
  • 56,621
  • 29
  • 151
  • 198