我有一个意图,从我的数据库通过长额外的信息。它还通过Activity设置String Extra的标题。
现在,当我第一次开始活动时,一切都完美地呈现出来。不过,我还有另一个活动用于添加新的数据库项。问题是,当我按下后退按钮(操作栏中的徽标,旁边有一个箭头)时,回到原来的活动,似乎附加功能消失了,因为标题和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);
    }

}

它现在什么也做不了。但就像我说的,当我点击back按钮(操作栏中的徽标)时,它会回到daysActivity,但是title和listView是空的。dayaddactivity的层次父级是daysactivity。
DayAddActivity将有一个“保存”按钮,该按钮将向数据库添加新的一天,然后finish()。如果我现在不解决这个问题,我想我会再次遇到这个问题。

最佳答案

你快把事情做好了。当您的活动即将结束时,您放置在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()中保存的附加功能。

07-25 21:32