I have 2 activities
1.Register.java
2.SecureXActivity.java
In Register activity i am doing registration of my application using shared preferences. I have used shared preferences to show Register activity only once. Now my requirement is take
user name from register activity which will be shown only once at the time of registration and save it in SecureXActivity activity, after the registration only this activity will be visible to user. So please help me in this regard? I am new to android.
My code is given below
Register.java
public class Register extends Activity
{
public static final String PREFS_NAME = "LoginPrefs";
public static final String USER_NAME = "USER";
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
/*
* Check if we successfully logged in before.
* If we did, redirect to home page
*/
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
if (settings.getString("logged", "").toString().equals("logged"))
{
Intent intent = new Intent(Register.this, SecureXActivity.class);
startActivity(intent);
}
Button b = (Button) findViewById(R.id.btnRegister);
b.setOnClickListener(new OnClickListener()
{
public void onClick(View v) {
EditText username = (EditText) findViewById(R.id.reg_employeeid);
EditText password = (EditText) findViewById(R.id.reg_password);
String welcm ="Welcome";
String space = " ";
EditText firstname = (EditText)findViewById(R.id.reg_firstname);
EditText lastname = (EditText)findViewById(R.id.reg_lastname);
String getfirstname = firstname.getText().toString();
String getlasttname = lastname.getText().toString();
String welcome=welcm.concat(space).concat(getfirstname).concat(space).concat(getlasttname);
if(username.getText().toString().length() > 0 && password.getText().toString().length() > 0 )
{
if(username.getText().toString().equals("test") && password.getText().toString().equals("test"))
{
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("logged", "logged");
editor.commit();
Intent intent = new Intent(Register.this, SecureXActivity.class);
intent.putExtra("name", welcome);
startActivity(intent);
}
}
}
});
}
}
SecureXActivity.java
public void onCreate(Bundle savedInstanceState)
{
Thread.setDefaultUncaughtExceptionHandler(new exceptionHandler(this));
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Bundle extras =getIntent().getExtras();
if (extras != null)
{
String value = extras.getString("name");
TextView txtwelcm= (TextView)findViewById(R.id.textViewwelcm);
txtwelcm.setText(value);
}
}
I want a value from one activity to another activity and value should be saved to another activity. In my above code i have done this using intent. But the problem is value is shown only once in second activity. Value is not visible when i press back button or home key?How to do this.
Added in response to @Picarus:
The problem is i am using same shared preferences to show the register activity only once.
But it not showing the name on SecureX activity.Is it possible to use same shared preference to carry value from register activity to securex activity
The Entire code is Given below once again
Register.java
public class Register extends Activity
{
public static final String PREFS_NAME = "LoginPrefs";
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
/*
* Check if we successfully logged in before.
* If we did, redirect to home page
*/
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
if (settings.getString("logged", "").toString().equals("logged"))
{
Intent intent = new Intent(Register.this, SecureXActivity.class);
startActivity(intent);
}
Button b = (Button) findViewById(R.id.btnRegister);
b.setOnClickListener(new OnClickListener()
{
public void onClick(View v) {
EditText username = (EditText) findViewById(R.id.reg_employeeid);
EditText password = (EditText) findViewById(R.id.reg_password);
String welcm ="Welcome";
String space = " ";
EditText firstname = (EditText)findViewById(R.id.reg_firstname);
EditText lastname = (EditText)findViewById(R.id.reg_lastname);
String getfirstname = firstname.getText().toString();
String getlasttname = lastname.getText().toString();
String welcome=welcm.concat(space).concat(getfirstname).concat(space).concat(getlasttname);
if(username.getText().toString().length() > 0 && password.getText().toString().length() > 0 )
{
if(username.getText().toString().equals("test") && password.getText().toString().equals("test"))
{
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("name", welcome);
editor.commit();
Intent intent = new Intent(Register.this, SecureXActivity.class);
//intent.putExtra("name", welcome);
startActivity(intent);
}
}
}
});
}
}
SecureXActivity.java
public class SecureXActivity extends Activity
{
public static final String PREFS_NAME = "LoginPrefs";
private ListView m_listview;
Button btnSendlogs;
Button btnMailadmin;
Button btnContactSupport;
Button btnSettings;
@Override
public void onCreate(Bundle savedInstanceState)
{
Thread.setDefaultUncaughtExceptionHandler(new exceptionHandler(this));
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Here we are setting the installation date of a application
ContextWrapper context = this;
PackageManager packageManager = context.getPackageManager();
ApplicationInfo appInfo;
try
{
appInfo = packageManager.getApplicationInfo("com.simsys.securex", 0);
String sourceDir= appInfo.sourceDir;
long dateTimeSync= new File(sourceDir).lastModified();
String DateTimeStamp=getDate(dateTimeSync, "dd/MM/yyyy hh:mm:ss");
String dateTimeSync2="Installed_Date:";
String dateTimeSync3=dateTimeSync2.concat(DateTimeStamp);
TextView txtCurrentTime= (TextView)findViewById(R.id.textViewdatetime);
txtCurrentTime.setText(dateTimeSync3);
}
catch (NameNotFoundException e)
{
e.printStackTrace();
}
//Here We Welcome the user
//List View
m_listview = (ListView) findViewById(R.id.list_view);
ArrayList<String> listItems=new ArrayList<String>();
listItems.add("CAMERA");
listItems.add("RECORDER");
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listItems);
m_listview.setAdapter(adapter);
if(isBluetoothServiceRunning())
{
listItems.add("BLUETOOTH");
}
if(isGprsServiceRunning())
{
listItems.add("GPRS");
}
if(isWifiServiceRunning())
{
listItems.add("WIFI");
}
adapter.notifyDataSetChanged();
//Call to Buttons Functionality
btnSendlogs = (Button) findViewById(R.id.button_sendlogs);
btnSendlogs.setOnClickListener(myhandler1);
btnMailadmin=(Button) findViewById(R.id.button_mailtoadmin);
btnMailadmin.setOnClickListener(MailadminHandler);
btnContactSupport = (Button) findViewById(R.id.button_contactsupport);
btnContactSupport.setOnClickListener(ContactSupportHandler);
}
//Navigation from Registration to Main page
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater Inflater = getMenuInflater();
Inflater.inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
if (item.getItemId() == R.id.logout)
{
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
String data2=settings.getString("name", "");
TextView txtwelcm= (TextView)findViewById(R.id.textViewwelcm);
txtwelcm.setText(data2);
SharedPreferences.Editor editor = settings.edit();
editor.remove("name");
editor.commit();
finish();
}
return super.onOptionsItemSelected(item);
}
For implementing sharedPreferences use following code
To retrieve from SharedPreferences