0

I have an Activity A with 2 tabs and each tab have its own Activity (B and C) . Activity B and Activity C each have 2 text fields . I want to save value of these text field in SharedPreferences when user changes tabs.

How can I do this?

Vivek Kalkur
  • 2,200
  • 2
  • 21
  • 40

1 Answers1

1

What you probably want is save your activity state in onSaveInstanceState instead, like this:

@Override
protected void onSaveInstanceState(Bundle outState) {
  super.onSaveInstanceState(outState);
  outState.putString(MY_KEY, myStringValue);
  // ...
}

And then in onCreate:

@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  // other stuff...

  if(savedInstanceState != null) {
      myStringValue = savedInstanceState.getString(MY_KEY);
  }

}
TotoroTotoro
  • 17,524
  • 4
  • 45
  • 76