brayo karis brayo karis - 4 years ago 99
Android Question

Saving data in multiple android activities

I wanted to know how one can save data on multiple activities during navigation.

I have 2 forms in my application,

account details
personal details
, each on its own activity. When the user clicks a
button in the
account details
activity, it takes him to the
personal details
activity. The
personal details
activity also has a back button to take the user back to the
account details

What I want is that when the user clicks the
button, it will save the data in the
account details
activity so that it may be displayed when the user returns to the
account details
activity. Also, when the user enters data in the
personal details
activity and probably moves back to the
account details
activity by clicking
, the data in the
personal details
activity should be saved so that when the user moves again to the
personal details
activity, he may find his data.

What I have done is override the
method and added some methods to save data in the
account details
activity as shown:

package com.example.karyukie.application;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class AccountDetails extends AppCompatActivity {

EditText usernameSignUp, newPwd, confirmPwd, securityA;
Spinner securityQ;

private static final String USERNAME_SIGN_UP = "usernameSignUp_key";
private static final String PWD_SIGN_UP = "pwdSignUp_key";
private static final String SECURITY_Q = "securityQ_key";
private static final String SECURITY_A = "securityA_key";

protected void onCreate(Bundle savedInstanceState) {
usernameSignUp = (EditText) findViewById(;
newPwd = (EditText) findViewById(;
confirmPwd = (EditText) findViewById(;
securityQ = (Spinner) findViewById(;
securityA = (EditText) findViewById(;


Button next = (Button) findViewById(;
assert next != null;
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Bundle accountDetails_data = saveData();
Intent intent = new Intent(getApplicationContext(), PersonalDetails.class);
startActivityForResult(intent, 1);

public Bundle saveData() {
String usernameSignUp_value = usernameSignUp.getText().toString();
String pwdSignUp_value = newPwd.getText().toString();
int securityQ_value = securityQ.getSelectedItemPosition();
String securityA_value = securityA.getText().toString();

Bundle data = new Bundle();

data.putString(USERNAME_SIGN_UP, usernameSignUp_value);
data.putString(PWD_SIGN_UP, pwdSignUp_value);
data.putInt(SECURITY_Q, securityQ_value);
data.putString(SECURITY_A, securityA_value);

return data;

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == 1) {
if(resultCode == Activity.RESULT_OK) {
securityQ.setSelection(data.getIntExtra(SECURITY_Q, 10));

And in the
personal details

Button back = (Button) findViewById(;
assert back != null;
back.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Bundle data = getIntent().getExtras();
Intent intent = new Intent();
setResult(Activity.RESULT_OK, intent);

It seems to work fine but I cannot figure how to save the data in the
personal details
activity as earlier described.

Answer Source

If you do not want to pass data using Intents. You can also use Shared Preferences to share data between activities.

You can get a preferences object by calling:

SharedPreferences sharedPreferences = context.getSharedPreferences();

Then (to use String data as an example) store using:

sharedPreferences.edit().putString("key", "value").apply();

and get data using:

sharedPreferences.getString("key", "default_value");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download