Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9034511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T08:29:54+00:00 2026-06-16T08:29:54+00:00

I have an Intent which gets information from my database through an long Extra.

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T08:29:56+00:00Added an answer on June 16, 2026 at 8:29 am

    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().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Class A which runs activity via startActivityForResult by passing Intent to
i have 2 classes. class1 gets some information through the net. when one of
I have an Activity (A) and a Service (S) which gets started by A
I have a main activity which calls a child one via Intent I =
I have an activity which gets called by 2 intents, one after a simple
I have my main project which gets images from certain sources (returns Uri). The
i have this IntentService code which run a service once: protected void onHandleIntent(Intent intent)
I have a Activity that pulls an Array from the previous Activity . I
I have an activity that is started from another activity via a button and
I have an android application that gets invoked through ADB on a desktop machine.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.