How to save the instancestate of my activity
I obviously new and have been trying to two days to figure out how to save the state of my main activity to no avail. I would appreciate any help. When I launch the ShowDetail activity and return to the main activity I have no data in the list. I have two xml files a main.xml and a item.xml file. main is just a listview and a textview. Item.xml is 3 textviews for the data in the list. Item Here is the code from my main activity:
public class main extends ListActivity {
private EventsData events;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
events = new EventsData(this);
try {
Cursor cursor = getEvents();
showEvents(cursor);
} finally {
events.close();
}
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState){
super.onSaveInstanceState(savedInstanceState);
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState){
super.onRestoreInstanceState(savedInstanceState);
}
@Override
public void onPause(){
super.onPause();
}
@Override
p开发者_开发问答ublic void onRestart(){
super.onRestart();
}
private static String[] FROM = { CODE, EXCERPT, _ID, };
private static String ORDER_BY = CODE + " ASC";
private Cursor getEvents() {
SQLiteDatabase db = events.getReadableDatabase();
Cursor cursor = db.query(TABLE_NAME, FROM, null, null, null, null, ORDER_BY);
startManagingCursor(cursor);
return cursor;
}
private static int[] TO = { R.id.code, R.id.excerpt, };
private void showEvents(Cursor cursor) {
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.item, cursor, FROM, TO);
setListAdapter(adapter);
}
private static String[] MIKEY = { _ID, CODE, DEFINITION };
protected void onListItemClick(ListView l, View v, int position, long id) {
Cursor cursor = ((CursorAdapter)getListAdapter()).getCursor();
cursor.getLong(2);
SQLiteDatabase db = events.getReadableDatabase();
Cursor c = db.query(TABLE_NAME, MIKEY, "_id = "+cursor.getLong(2)+"", null, null, null, null);
c.moveToFirst();
Intent in1 = new Intent();
Bundle bun = new Bundle();
bun.putLong("id", c.getLong(0));
bun.putString("code", c.getString(1));
bun.putString("definition", c.getString(2));
in1.setClass(this, ShowDetail.class);
in1.putExtras(bun);
startActivity(in1);
}
}
I'd say you need to place your general actions into onResume()
instead of in onCreate()
.
Maybe a look at the application lifecycle helps understanding what I mean: http://developer.android.com/reference/android/app/Activity.html
精彩评论