Reputation: 5896
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState)
{
String phonenumber = phoneNumberLogin.getText().toString();
savedInstanceState.putString("PhoneNumber", phonenumber);
super.onSaveInstanceState(savedInstanceState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
// TODO Auto-generated method stub
String phonenumber= savedInstanceState.getString("PhoneNumber");
if (phonenumber != null) {
EditText FirstName = (EditText) findViewById(R.id.editTextname);
FirstName.setText(phonenumber);
}
super.onRestoreInstanceState(savedInstanceState);
}
I have one editText (for typing phone number). When in portrait mode I type the numbers and after that rotating the phones, the typed data is gone. I save data savedInstanceState but I have no idea how can I restore my typed data when I rotate the phone. Any help please.
Upvotes: 0
Views: 549
Reputation: 3822
try to move getting phone number from onRestoreInstanceState to onCreate method or try something like this:
@Override
public Object onRetainNonConfigurationInstance()
{
return phoneNumberLogin.getText().toString();
}
and in onCreate
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
String phonenumber= (String)getLastNonConfigurationInstance();
if (phonenumber != null) {
EditText FirstName = (EditText) findViewById(R.id.editTextname);
FirstName.setText(phonenumber);
}
}
Upvotes: 0
Reputation: 17317
On simple way is to avoid having your activity recreated on rotation. Change your AndroidManifest.xml to include android:configChanges="keyboardHidden|orientation"
like this:
<activity android:name="MyActivity" android:configChanges="keyboardHidden|orientation"></activity>
Your layout will need to support whatever orientation the device supports. Also there are some other things to consider: Why not use always android:configChanges="keyboardHidden|orientation"?
Upvotes: 1