Skip to content

Instantly share code, notes, and snippets.

@evanhalley
Created March 6, 2015 01:41
Show Gist options
  • Save evanhalley/72a00a3ba856691960c2 to your computer and use it in GitHub Desktop.
Save evanhalley/72a00a3ba856691960c2 to your computer and use it in GitHub Desktop.
Implementing onSavedInstanceState.
package com.example.android.sampleform;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.EditText;
/**
* Demonstrates the activity lifecycle and saving data before the activity is destroyed
*/
public class MainActivity extends ActionBarActivity {
private final static String TAG = MainActivity.class.getSimpleName();
@Override
public void onSaveInstanceState(Bundle outState) {
Log.d(TAG, "Saving instance state");
outState.putString("name", ((EditText) findViewById(R.id.name)).getText().toString());
outState.putString("address", ((EditText) findViewById(R.id.address)).getText().toString());
outState.putString("phone", ((EditText) findViewById(R.id.phone)).getText().toString());
outState.putString("email", ((EditText) findViewById(R.id.email)).getText().toString());
}
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.d(TAG, "Create");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState != null) {
Log.d(TAG, "Repopulating form");
((EditText) findViewById(R.id.name)).setText(savedInstanceState.getString("name"));
((EditText) findViewById(R.id.address)).setText(savedInstanceState.getString("address"));
((EditText) findViewById(R.id.phone)).setText(savedInstanceState.getString("phone"));
((EditText) findViewById(R.id.email)).setText(savedInstanceState.getString("email"));
}
}
@Override
protected void onStart() {
Log.d(TAG, "Start");
super.onStart();
}
@Override
protected void onResume() {
Log.d(TAG, "Resume");
super.onResume();
}
@Override
protected void onPause() {
Log.d(TAG, "Pause");
super.onPause();
}
@Override
protected void onStop() {
Log.d(TAG, "Stop");
super.onStop();
}
@Override
protected void onDestroy() {
Log.d(TAG, "Destroy");
super.onDestroy();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment