0

How to send string variable from the first activity to the fourth activity ?

I got 2 string variables on my first activity, I want to pass them to the fourth activity. How would I fetch the data if I use this syntax ?

    Public class FirstActivity extends AppCompatActivity {
        public static String first_string ;
        public static String last_string ;

Is there a better way ?

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
AtonfO64
  • 21
  • 2
  • 1
    Possible duplicate of [How do I pass data between activities on Android?](http://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-on-android) – OneCricketeer May 12 '16 at 02:56

2 Answers2

1

There are two ways to do this 1) Use Intent method.

In the first activity intent.putExtra("firststring",firststring);

In the fourth activity

String first = getIntent().getExtras().getString("firststring");

So similiar process in 2nd 3rd activity.

2) Use sharedpreference method

In the first activity

public static final String MyPREFERENCES = "MyPrefs";
    SharedPreferences sharedpreferences;
sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit(); editor.putString("firststring", firststring);

In the fourth activity

public static final String MyPREFERENCES = "MyPrefs";


SharedPreferences sharedpreferences;
sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);

String firststing = sharedpreferences.getString("firststring", "firststring");
Vishwesh Jainkuniya
  • 2,821
  • 3
  • 18
  • 35
0

As of right now you could just access them from FourthActivity with:

String first_string_new = FirstActivity.first_string;

But, I would recommend you to avoid static values and pass them as extras with used Intent.

From FirstActivity:

intent.putExtra("FIRST_STRING", first_string);
intent.putExtra("SECOND_STRING", last_string);
startActivity(intent);

Receive the strings in the FourthActivity:

Bundle extras = getIntent().getExtras();
String first_string_new = extras.getString("FIRST_STRING");
String second_string_new = extras.getString("SECOND_STRING");

Check this question to see another example using extras in the Intent.

Community
  • 1
  • 1
Evin1_
  • 12,292
  • 9
  • 45
  • 47
  • I want to know, will the startactivity(intent) line directly start activity4 ? – AtonfO64 May 12 '16 at 03:03
  • Yes, if you create the Intent as: `Intent intent = new Intent(this, FourthActivity.class);` – Evin1_ May 12 '16 at 03:04
  • But I don't want to, so how will I process to get to activities 2&3 before activity4 ? – AtonfO64 May 12 '16 at 03:11
  • I would still try to avoid static variables or Singletons, and pass the strings via extras from Activity1 to Activity2, from Activity2 to Activity 3 and finally to Activity4, your testing frameworks will be easier to implement if you avoid the approaches mentioned at the beginning. – Evin1_ May 12 '16 at 03:14
  • I was thinking maybe it was a shortcut. Thanks to help – AtonfO64 May 12 '16 at 03:19