额外的东西似乎在resume()上丢失了



我有一个意图,通过一个长Extra从我的数据库获取信息。它还通过String Extra设置Activity的标题。

现在,当我第一次启动活动,一切都显示完美。然而,我有另一个活动,我用它来添加新的数据库项目。问题是,当我按下后退按钮(在操作栏的标志,旁边有一个箭头),回到原来的活动,似乎好像额外的消失了,因为标题和ListView是空的。

这是一个活动,显示ListView没有问题时启动:

DaysActivity.java:
package com.gauvion.gfit;
import android.annotation.SuppressLint;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
public class DaysActivity extends ListActivity {
    private DaysDataSource datasource;
    private SimpleCursorAdapter dataAdapter;
    private boolean isEditing = false;
    private Toast toast_deleted;
    private String[] columns = new String[] { MySQLiteHelper.COLUMN_NAME, MySQLiteHelper.COLUMN_DAY };
    private int[] to;
    private long routineDataID;
    private String routineDataName;
    @SuppressLint("ShowToast")
    @Override
    public void onCreate(Bundle savedInstanceState) {       
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_days);
        routineDataID = getIntent().getLongExtra("routineDataID", 1);
        routineDataName = getIntent().getStringExtra("routineDataName");
        setTitle(routineDataName);
        toast_deleted = Toast.makeText(this, "", Toast.LENGTH_SHORT);
        datasource = new DaysDataSource(this);
        datasource.open();
        Cursor cursor = datasource.fetchAllDays(routineDataID);
        to = new int[] { R.id.listitem_day_name, R.id.listitem_day_day };
        dataAdapter = new SimpleCursorAdapter(this, R.layout.listitem_day, cursor, columns, to, 0);
        setListAdapter(dataAdapter);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {     
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.activity_days, menu);
        return true;        
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {     
        Cursor cursor = datasource.fetchAllDays(routineDataID);
        switch (item.getItemId()) {
            case R.id.button_days_add:
                Intent startDayAdd = new Intent(this, DayAddActivity.class);
                startDayAdd.putExtra("routineDataID", routineDataID);
                this.startActivity(startDayAdd);
                return true;
            case R.id.button_days_edit:
                to = new int[] { R.id.listitem_day_edit_name, R.id.listitem_day_edit_day };
                dataAdapter = new SimpleCursorAdapter(this, R.layout.listitem_day_edit, cursor, columns, to, 0);
                setListAdapter(dataAdapter);
                isEditing = true;
                invalidateOptionsMenu();
                return true;
            case R.id.button_days_edit_done:
                to = new int[] { R.id.listitem_day_name, R.id.listitem_day_day };
                dataAdapter = new SimpleCursorAdapter(this, R.layout.listitem_day, cursor, columns, to, 0);
                setListAdapter(dataAdapter);
                isEditing = false;
                invalidateOptionsMenu();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        super.onPrepareOptionsMenu(menu);
        menu.findItem(R.id.button_days_edit).setVisible(!isEditing);
        menu.findItem(R.id.button_days_edit_done).setVisible(isEditing);
        return true;
    }
    @Override
    protected void onResume() {
        datasource.open();
        Cursor cursor = datasource.fetchAllDays(routineDataID);
        dataAdapter.changeCursor(cursor);
        super.onResume();
    }
    @Override
    protected void onPause() {
        datasource.close();
        super.onPause();
    }
    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
      super.onSaveInstanceState(savedInstanceState);
      savedInstanceState.putLong("routineDataID", routineDataID);
      savedInstanceState.putString("routineDataName", routineDataName);
    }
    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
      super.onRestoreInstanceState(savedInstanceState);
      routineDataID = savedInstanceState.getLong("routineDataID");
      routineDataName = savedInstanceState.getString("routineDataName");
    }
} 

你可以看到我甚至尝试使用onSaveInstanceState()onRestoreInstanceState()。也许我没有正确地使用它们?

下面是我将用来添加新天数的活动:

DayAddActivity.java:

package com.gauvion.gfit;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.app.NavUtils;
public class DayAddActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_day_add);
        getActionBar().setDisplayHomeAsUpEnabled(true);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_day_add, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            NavUtils.navigateUpFromSameTask(this);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

它现在不做任何事情。但就像我说的,当我点击后退按钮(在操作栏的标志),它回到DaysActivity,但标题和ListView是空的。DayAddActivity的层次父类是DaysActivity。

DayAddActivity将有一个"保存"按钮,将添加新的一天到数据库,然后finish()。如果我现在不解决这个问题,我想我还会遇到这个问题。

你几乎做对了。当活动结束时,您在onSaveInstanceState()中放置的所有内容都必须在onCreate()方法中的Activity的新实例中获得。

应该是这样的:

@SuppressLint("ShowToast")
@Override
public void onCreate(Bundle savedInstanceState) {       
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_days);
    Bundle bundle = savedInstanceState != null ? savedInstanceState : getIntent().getExtras();
    routineDataID = bundle.getLongExtra("routineDataID", 1);
    routineDataName = bundle.getStringExtra("routineDataName");
    setTitle(routineDataName);
    //other stuff
}

所以,基本上getIntent()方法是在第一次显示Activity时使用的。之后,您将获得您保存在onSaveInstanceState()中的额外内容。

相关内容

  • 没有找到相关文章

最新更新