Passing data between Activity Android

Asked

Viewed 1,211 times

5

Well, I’m making an app and I have a login screen that connects to the database from a web service, only q I’m having a problem want to get the user id and user name to pass this data to another Activity and I’m not getting .. Because when the person is going to publish something precise that saves in the database the id and the name of the person, some suggestion of how to do this ?

2 answers

7


Use Intents.

In his Activity 1, use a code more or less like this:

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

startActivity(i);

To recover 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");
}

5

If you are going to use only the Bundle / Intent only once will do, now if you want to keep them and recover later on any Sharedpreferences use

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 look even more beautiful you can create your preference 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 of use:

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

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

It’s all static even then just use, I hope I helped.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.