0

if I want to send the same data to more than one Activity for example this code:

Intent passDataToSomeScreen = new Intent(MainActivity.this,
                                         Second_Screen.class);
String first_name = etFirstName.getText().toString();
passDataToSecondScreen.putExtra("FIRST_NAME", first_name);

String last_name = etLastName.getText().toString();
passDataToSecondScreen.putExtra("LAST_NAME", last_name);

startActivity(passDataToSomeScreen);

But instead of going to the second screen, I want to send it also to third screen, forth etc.. (concurrent...) than to get the same data on each screen

Can it be done from one Intent?

Sufian
  • 6,405
  • 16
  • 66
  • 120
  • you mean you want to start multiple activities at once? – Mohammad Rahchamani Jun 12 '16 at 07:27
  • 1
    I think the answer you looking for can be found here http://stackoverflow.com/questions/7885276/how-to-share-same-data-between-multiple-activities-in-android – Twahanz Jun 12 '16 at 07:29
  • 1
    Have you tried broadcast listeners? – insomniac Jun 12 '16 at 07:34
  • @insomniac as far as only one activity is active, how can we do this using broadcast listeners? – Mohammad Rahchamani Jun 12 '16 at 07:36
  • Does it matter? From the question,I've got a feeling that the data is irrelevant when the activity is not active,Otherwise he could have used a background service right? – insomniac Jun 12 '16 at 07:38
  • From the vague information given in the question,Are trying to create a wizard like behavior in the application ? – insomniac Jun 12 '16 at 07:41
  • 1
    @insomniac I don't know about the behavior required in this question. but I think we can't use broadcast listeners here because activities are not active. but it's a good idea for example in fragments when you could have multiple available fragments at the moment. – Mohammad Rahchamani Jun 12 '16 at 07:48
  • 1
    I agree, Still the data is irrelevant even if we insist on using activities without fragments. The problem is the guy who asked may not yet have enough knowledge about fragments – insomniac Jun 12 '16 at 07:52

4 Answers4

1

If you want to store send data to other second,third,fourth screens etc. Then I think its better to save them in SharedPreferences.

SharedPreferences prefs=PreferenceManager.getDefaultSharedPreferences(getApplicationContext(),Context.PRIVATE);

prefs.get<TYPE>(<KEY>,<DEFAULT VALUE>)//FOR STORING DATA
prefs.edit().put<TYPE>(<KEY>,<VALUE>).apply();//FOR EDITING DATA
Yash Jain
  • 376
  • 1
  • 3
  • 13
0

Yes, it's easy. Put all your data in bundle, and in next activity just get the bundle and again put in the new intent you are using to launch the third activity.

        Bundle bundle = new Bundle();
        bundle.putString("FIRST_NAME", first_name);
        bundle.putString("LAST_NAME", last_name);
        passDataToSecondScreen.putExtra("DATA", bundle);

In next screen :

        passDataToThirdScreen.putExtra("DATA", getIntent().getBundleExtra("DATA"));

Hope it will work :)

Neo
  • 3,546
  • 1
  • 24
  • 31
0

You can use Java Singleton Design pattern to achieve this without shared preferences :-

Set UserName from any class/Activity/Fragment like this

     //Set user name
    SessionManager.getInstance().setFirstName("UserName");

Get UserName in any class/Activity/Fragment like this

    //Get username
    String UserName =  SessionManager.getInstance().getFirstName();

Dont forgot to clear session when all use is done

    //When you are done clear session
    SessionManager.getInstance().flushSesion();

Session manager clas would look like this

public class SessionManager {
    private static SessionManager ourInstance = new SessionManager();

    private String firstName;
    private String lastName;

    public static SessionManager getInstance() {
        return ourInstance;
    }

    private SessionManager() {
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public void flushSesion() {

        firstName = null;
        lastName = null;
    }

}
Hitesh Sahu
  • 41,955
  • 17
  • 205
  • 154
0

One short way can be this. To use static variables in class. Create class Helper

      public  class  Helper {
               public static String FIRST_NAME;
               public static String  LAST_NAME;

       }

Set like this Helper.FIRST_NAME="foo"; Acess like this

                 String someName=Helper.FIRST_NAME;
Usman lqbal
  • 935
  • 1
  • 9
  • 31