Passing data between Activity Android

4

Well, I'm making an app and I have a login screen that connects to the database from a web service, but I have a problem, I want to get the user id and user name to pass these Data for another Activity and I am not getting .. For when the person is going to publish something I need to save in the database the id and the name of the person, any suggestions how to do this?

    
asked by anonymous 13.05.2016 / 20:43

2 answers

5

Use Intents .

In your Activity 1 , use a code something like this:

Intent i = new Intent(getApplicationContext(), NewActivity.class);
i.putExtra("id_pessoa", "1234");
i.putExtra("nome_pessoa", "John Doe");

startActivity(i);

To retrieve the data in your Activity 2 :

Bundle extras = getIntent().getExtras();

if (extras != null) {
    Integer idPessoa = extras.getInt("id_pessoa");
    String nomePessoa = extras.getString("nome_pessoa");
}
    
13.05.2016 / 20:55
4

If you are going to use it once, the Bundle / Intent will serve, now if you want to keep them and then recover in any activity use SharedPreferences

SharedPreferences pref = getApplicationContext().getSharedPreferences("MYPREF", 0); 

Editor editor = pref.edit(); 

To enter the data use

editor.putInt("key_name", "value");
editor.putString("key_name", "value");

and so on.

To recover the data you do the following

String string = pref.getString("key_name", null);

int x = pref.getInt("key_name", null); 

To be more beautiful still you can create your preferences class

public class Preferences {

public static final String PREF_NAME = "your preferences name";

@SuppressWarnings("deprecation")
public static final int MODE = Context.MODE_WORLD_WRITEABLE;

public static final String USER_ID = "USER_ID_NEW";
public static final String USER_NAME = "USER_NAME";

public static final String NAME = "NAME";
public static final String EMAIL = "EMAIL";
public static final String PHONE = "PHONE";
public static final String address = "address";

public static void writeBoolean(Context context, String key, boolean value) {
getEditor(context).putBoolean(key, value).commit();
}

public static boolean readBoolean(Context context, String key,
    boolean defValue) {
return getPreferences(context).getBoolean(key, defValue);
}

public static void writeInteger(Context context, String key, int value) {
getEditor(context).putInt(key, value).commit();

}

public static int readInteger(Context context, String key, int defValue) {
return getPreferences(context).getInt(key, defValue);
}

public static void writeString(Context context, String key, String value)   {
getEditor(context).putString(key, value).commit();

}

public static String readString(Context context, String key, String defValue) {
return getPreferences(context).getString(key, defValue);
}

public static void writeFloat(Context context, String key, float value) {
getEditor(context).putFloat(key, value).commit();
}

public static float readFloat(Context context, String key, float defValue) {
return getPreferences(context).getFloat(key, defValue);
}

public static void writeLong(Context context, String key, long value) {
getEditor(context).putLong(key, value).commit();
}

public static long readLong(Context context, String key, long defValue) {
return getPreferences(context).getLong(key, defValue);
}

public static SharedPreferences getPreferences(Context context) {
return context.getSharedPreferences(PREF_NAME, MODE);
}

public static Editor getEditor(Context context) {
return getPreferences(context).edit();
}

}

Example usage:

Preferences.writeString(getApplicationContext(),
                Preferences.NAME, "dev");

Preferences.readString(getApplicationContext(), Preferences.NAME,
                "");

It's all static even just use, I hope I have helped.

    
13.05.2016 / 22:53