如何保存我的活动的实例状态

时间:2010-10-24 02:46:02

标签: android listview android-activity listactivity

我显然很新,并且已经尝试了两天来弄清楚如何保存我的主要活动的状态无济于事。我将不胜感激任何帮助。当我启动ShowDetail活动并返回主活动时,列表中没有数据。我有两个xml文件,一个main.xml和一个item.xml文件。 main只是一个列表视图和一个textview。 Item.xml是列表中数据的3个文本视图。项目以下是我主要活动的代码:

public class main extends ListActivity {

私人EventsData事件;

/** 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
public 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);
}

}

1 个答案:

答案 0 :(得分:0)

我说你需要将一般行动放在onResume()而不是onCreate()

也许看看应用程序生命周期有助于理解我的意思: http://developer.android.com/reference/android/app/Activity.html