scarhand
scarhand

Reputation: 4337

Extras seem to be lost onResume()

I have an Intent which gets information from my database through an long Extra. It also sets the title of the Activity through a String Extra.

Now when I first start the Activity, everything shows up perfectly. However I have another Activity which I use to add new database items. The problem is, when I press the back button (the logo in the action bar with an arrow beside it), going back to the original Activity, it seems as if the Extras are gone because the title and ListView is empty.

Here is the Activity that displays the ListView with no problems when started:

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");
    }

} 

As you can see I've even tried using onSaveInstanceState() and onRestoreInstanceState(). Maybe I'm not using them correctly?

Here is the activity that I am going to use to add new days:

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);
    }

}

It doesn't do anything right now. But like I said, when I click the back button (the logo in the action bar) it goes back to DaysActivity but the title and ListView is empty. The hierarchical parent for DayAddActivity is DaysActivity.

DayAddActivity will have a "Save" button which will add a new day to the database and then finish(). I'm assuming I will run into this problem again if I don't get this resolved now.

Upvotes: 3

Views: 5897

Answers (1)

Snicolas
Snicolas

Reputation: 38168

You are almost getting it right. Everything you place in onSaveInstanceState() when you activity is dying must be obtained in a new instance of your Activity inside the onCreate() method.

You should have something like :

@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
}

So, yes basically the getIntent() method is used when first displaying your Activity. Afterwards you will get the extras that you saved in onSaveInstanceState().

Upvotes: 6

Related Questions