Basic Android
Basic Android

Reputation: 51

Intent to send data to several screens concurrent

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?

Upvotes: 0

Views: 72

Answers (4)

Usman lqbal
Usman lqbal

Reputation: 974

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;

Upvotes: 0

Hitesh Sahu
Hitesh Sahu

Reputation: 45072

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;
    }

}

Upvotes: 0

Neo
Neo

Reputation: 3584

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 :)

Upvotes: 0

Yash Jain
Yash Jain

Reputation: 374

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

Upvotes: 1

Related Questions