Anca Ioana Grigore
Anca Ioana Grigore

Reputation: 29

Text added in textview remain there after user signs out

I have an app that allows sb to make an account and on the first page of their profile I send data from some edittexts from another activity to some textviews. Now what I need is that information (first name, last name and age) to remain there after log out. When the user logs in again, he should be able to see the information on his account's first page. How can I do this? Thanks. Here is the code:

public class AccountInformationSetter extends AppCompatActivity {
    EditText etFirstName, etLastName, etAge;
    Button btnSave;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_account_information_setter);

        etFirstName = (EditText) findViewById(R.id.etFirstName);
        etLastName = (EditText) findViewById(R.id.etLastName);
        etAge = (EditText) findViewById(R.id.etAge);
        btnSave = (Button) findViewById(R.id.btnSaveInfo);

        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String first = etFirstName.getText().toString();
                final String last = etLastName.getText().toString();
                final String age = etAge.getText().toString();

                Intent intent = new Intent(AccountInformationSetter.this, Profile.class);
                intent.putExtra("firstName", first);
                intent.putExtra("lastName", last);
                intent.putExtra("age", age);
                startActivity(intent);
            }
        });
    }

And this is the code for the first page of the activity:

public class Profile extends AppCompatActivity {
    TextView tv, tvFirstName, tvLastName, tvAge ;
    UserSessionManager session;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile);

        Intent in = getIntent();
        String tv1 = in.getExtras().getString("name");
        tv = (TextView) findViewById(R.id.textView);
        tv.setText(tv1);

        Intent intent = getIntent();
        String first = in.getExtras().getString("firstName");
        tvFirstName = (TextView) findViewById(R.id.tvFirstName);
        tvFirstName.setText("First name: " + first);

        Intent intent1 = getIntent();
        String last = in.getExtras().getString("lastName");
        tvLastName = (TextView) findViewById(R.id.tvLastName);
        tvLastName.setText("Last name: "+ last);

        Intent intent2 = getIntent();
        String age = in.getExtras().getString("age");
        tvAge = (TextView) findViewById(R.id.tvAge);
        tvAge.setText("Age: " + age);


    }

Upvotes: 0

Views: 50

Answers (3)

annapoorna
annapoorna

Reputation: 46

Use SharedPreferences to store the values. Next time, when the user logs in, just retrieve the stored values and display. This link would help: http://www.tutorialspoint.com/android/android_shared_preferences.htm

Upvotes: 0

simekadam
simekadam

Reputation: 7384

Does it mean it has to persist even app reinstall? Or login on another device?

  • If not, just use SharedPreferences, that is the simplest and clean solution.
  • If the answer is yet, you can either leverage things like the M backup (works only for Marshmallow and newer), or you would have to build some backend. Then it starts to be way more complicated and out of scope for such a generic question.

Upvotes: 0

Raghavendra B
Raghavendra B

Reputation: 441

Use SharedPreferences to store entered values on particular event.Then when again logged in check whether shared presence exist if yes retrieve data from SharedPreferences and populate to editText

Upvotes: 1

Related Questions