package com.trid.sharedtask;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
TextView userName, password;
Button ok, cancel;
SharedPreferences shared;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
userName = (TextView) findViewById(R.id.userName);
password = (TextView) findViewById(R.id.password);
ok = (Button) findViewById(R.id.ok);
cancel = (Button) findViewById(R.id.cancel);
userName.setText(shared.getString("key1", ""));
password.setText(shared.getString("key1", ""));
ok.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), Activity2.class);
startActivity(intent);
SharedPreferences shared = getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = shared.edit();
editor.putString("key1", userName.getText().toString().trim());
editor.putString("key2", password.getText().toString().trim());
editor.commit();
}
}
I have to create a login page using shared preference. if the user enters first time in a activity, the username and password should be stored in shared preference. if the user enters in consecutive time, the shared preference fetch the data to username and password.
Thanks for your Help...!