0
@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.

dbr
  • 165,801
  • 69
  • 278
  • 343
fish40
  • 5,738
  • 17
  • 50
  • 69

2 Answers2

1

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"?

Community
  • 1
  • 1
ThomasW
  • 16,981
  • 4
  • 79
  • 106
0

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);
   }
}
Vladimir
  • 3,774
  • 3
  • 23
  • 27