额外的东西似乎丢失了onResume()

时间:2012-12-26 13:37:31

标签: android android-intent android-activity extra

我有一个Intent,通过一个很长的Extra从我的数据库中获取信息。它还会将Activity的标题设置为String Extra

现在,当我第一次启动活动时,一切都完美呈现。但是我有另一个Activity用于添加新的数据库项。问题是,当我按下后退按钮(操作栏中的徽标旁边有一个箭头)时,返回到原始活动状态,似乎额外内容已经消失,因为标题和ListView为空

这是在启动时显示ListView而没有任何问题的Activity:

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()。我假设如果我现在没有解决这个问题,我会再次遇到这个问题。

1 个答案:

答案 0 :(得分:6)

你几乎是正确的。当您的活动正在消亡时,您在onSaveInstanceState()中放置的所有内容都必须在Activity方法中onCreate()的新实例中获取。

你应该有类似的东西:

@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()中保存的额外内容。