Skip to content

Instantly share code, notes, and snippets.

@OrenBochman
Last active August 28, 2021 08:08
Show Gist options
  • Save OrenBochman/34c1adcd61b7f052d022553cb5b190a5 to your computer and use it in GitHub Desktop.
Save OrenBochman/34c1adcd61b7f052d022553cb5b190a5 to your computer and use it in GitHub Desktop.
Android State - save and restore activity's state in a bundle

Saving and restoring activity state across configuration changes

  1. implement onSaveInstanceState(Bundle state)
  2. implement onRestoreInstanceState(Bundle state)
  • input controlls are saved automatically
  • listview items/position etc need to be saved.
  • to persist for the long term use SQLite db, shared prefrences or a file
package com.example.user.arrayadapter1;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private ArrayAdapter<Person> listAdapter;
private ListView listView;
private EditText editText;
private ArrayList<Person> people;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.addButton);
button.setOnClickListener(this);
// Creating a list of initialization data for the list view
// We're using composite objects - Person
people = new ArrayList();
people.add(new Person("Eli", 10));
people.add(new Person("Dani", 11));
people.add(new Person("Rani", 12));
// Creating an array adapter (INIT DATA IS SUPPLIED AT THE END OF THE LINE)
// The title of each item in the listview, is decided by the value a Person's toString() method
// The listview calls the toString() method of its items, and uses the returned string as
// the name of the item in the list
listAdapter = new ArrayAdapter<Person>(this, android.R.layout.simple_list_item_1, people);
// Connecting the array adapter to the listview
listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(listAdapter);
// this.editText = (EditText) findViewById(R.id.editText1);
// Registering the list view to a single short click event (callback)
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Person person = (Person) parent.getItemAtPosition(position);
Toast.makeText(getApplicationContext(), person.getName()+ " " + person.getAge(), Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void onClick(View view) {
// Getitng a reference to the UI item where the user write names
TextView textView = (TextView) findViewById(R.id.editText1);
// Extracting the name the user wrote
String name = textView.getText().toString();
// Creating a person (hard coded age - 20)
Person person = new Person(name, 20);
// Adding the user to the names list (ArrayList)
people.add(person);
// Clearing the area where the user writes
textView.setText("");
// Updating the list (a classing Model - View interaction + Observer design pattern)
listAdapter.notifyDataSetChanged();
}
@Override
public void onSaveInstanceState(Bundle state) {
super.onSaveInstanceState(state);
state.putSerializable("people", people);
}
@Override
public void onRestoreInstanceState(Bundle state) {
super.onRestoreInstanceState(state);
this.people.clear();
this.people.addAll((ArrayList<Person>) state.getSerializable("people"));
this.listAdapter.notifyDataSetChanged();
}
}
package com.example.user.arrayadapter1;
import java.io.Serializable;
public class Person implements Serializable{
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return this.name;
}
}
@jian6137
Copy link

Thanks

@jian6137
Copy link

Thanks

Great

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment