Created
December 22, 2015 02:30
-
-
Save VladSem/8bc66c5faa13f14f514b to your computer and use it in GitHub Desktop.
Android, onSaveInstanceState and onActivityCreated in fragments
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class FragmentTab2 extends Fragment { | |
String msg = "Android : "; | |
Button buttonON; | |
TextView showIP1; | |
TextView showIP2; | |
@Override | |
public View onCreateView(LayoutInflater inflater,ViewGroup container, | |
Bundle savedInstanceState) { | |
View view = inflater.inflate(R.layout.fragmenttab2, container, false); | |
Log.d(msg, "The onCreateView() event"); | |
buttonON = (Button) view.findViewById(R.id.buttonON); | |
buttonON.setTransformationMethod(null); | |
showIP1 = (TextView) view.findViewById(R.id.showMyIP1tab2); | |
showIP2 = (TextView) view.findViewById(R.id.showMyIP2tab2); | |
return view; | |
} | |
@Override | |
public void onSaveInstanceState(Bundle outState) { | |
super.onSaveInstanceState(outState); | |
String stateToSave = showIP1.getText().toString(); | |
String stateToSave2 = showIP2.getText().toString(); | |
String stateToSave3 = buttonON.getText().toString(); | |
outState.putString("savedState", stateToSave); | |
outState.putString("savedState2", stateToSave2); | |
outState.putString("savedState3", stateToSave3); | |
} | |
@Override | |
public void onActivityCreated(Bundle savedInstanceState) { | |
super.onActivityCreated(savedInstanceState); | |
String stateSaved; | |
String stateSaved2; | |
String stateSaved3; | |
if (savedInstanceState != null) { | |
stateSaved = savedInstanceState.getString("savedState"); | |
stateSaved2 = savedInstanceState.getString("savedState2"); | |
stateSaved3 = savedInstanceState.getString("savedState3"); | |
showIP1.setText(stateSaved); | |
showIP2.setText(stateSaved2); | |
buttonON.setText(stateSaved3); | |
} | |
} | |
@Override | |
public void onStart() { | |
super.onStart(); | |
Log.d(msg, "The onStart() event"); | |
} | |
@Override | |
public void onResume() { | |
super.onResume(); | |
Log.d(msg, "The onResume() event"); | |
} | |
@Override | |
public void onPause() { | |
super.onPause(); | |
Log.d(msg, "The onPause() event"); | |
} | |
@Override | |
public void onStop() { | |
super.onStop(); | |
Log.d(msg, "The onResume() event"); | |
} | |
@Override | |
public void onDestroy() { | |
super.onDestroy(); | |
Log.d(msg, "The onDestroy() event"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment