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

  • Home
  • SEARCH
  • 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 7669229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:32:53+00:00 2026-05-31T15:32:53+00:00

package com.commonsware.cwac.wakeful.demo; import android.app.ListActivity; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.provider.BaseColumns; import android.util.Log;

  • 0
package com.commonsware.cwac.wakeful.demo;

import android.app.ListActivity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.provider.BaseColumns;
import android.util.Log;
import android.widget.SimpleCursorAdapter;

public class FlightListActivity extends ListActivity {

private SQLiteDatabase database;
private String fields[] = {BaseColumns._ID, "name", "flights", "distance"};
private SimpleCursorAdapter dataSource;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.v(ACTIVITY_SERVICE, "onCreate flights");
    database = (SQLiteDatabase) getLastNonConfigurationInstance();
    if (database == null) {
        database = getData();
        Log.v(ACTIVITY_SERVICE, "first load data");
    }

    Cursor data = database.query("pilots", fields, null, null, null, null, null);

    dataSource = new CustomCursorAdapter(this, R.layout.row, data, fields, new int[] { R.id.id, R.id.name, R.id.flights, R.id.distance });
    setListAdapter(dataSource);
}

protected SQLiteDatabase getData() {
    DataBaseHelper myDbHelper = new DataBaseHelper(this.getApplicationContext());
    return myDbHelper.openDataBase();
}

@Override
public Object onRetainNonConfigurationInstance() {
    Log.v(ACTIVITY_SERVICE, "reusing data");
    final SQLiteDatabase myData = database;
    return myData;
}

@Override
protected void onDestroy() {
    database.close();
    super.onDestroy();
}

}

Ok so I am trying to hold on to my database by storing it in the onRetainNonConfigurationInstance, but I get a runtime error if I do not close the database in the onDestroy method.

What I don’t get it, if I close the database in onDestroy, then I need to reopen it again somewhere, but doesn’t that defeat the object of holding on to it in the first place?

Is this the best way to reuse the database when user rotates the device?

  • 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-05-31T15:32:54+00:00Added an answer on May 31, 2026 at 3:32 pm

    In general, don’t worry about this stuff – Android as actually very efficient when it comes to doing this sort of thing. Everything has to be recreated from scratch so in most cases, just let it happen.

    Opening the DB has a minimal cost in comparison to a query on it which might return a huge number of results. In saying that, that’s the point of a Cursor in that it is designed to handle the results from a query in an efficient manner.

    As for ListViews they only ever have a finite number of ‘items’ at any one time which are recycled as the ListView is scrolled and during an orientation change, the ListView will need to be re-drawn usually with a different number of visible list items.

    The use of onRetainNonConfigurationInstance() is a special case designed for more complex scenarioss – perhaps retain a canvas the user is drawing on, or ‘live’ network connections which are maintaining some sort of session state (authentication tokens etc).

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

Sidebar

Related Questions

package com.Kiro.Test; import android.app.Activity; import android.content.ContentResolver; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.provider.Contacts.People;
package com.example.Calc; import android.R.string; import android.app.ListActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.ArrayAdapter;
package com.commonsware.android.layouts; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import java.util.Date; public class
package com.comboyz.TantaGo; import android.app.ListActivity; import android.os.Bundle; import android.widget.ArrayAdapter; public class startMenu extends ListActivity{ @Override
package com.commonsware.android.threads; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.view.View.OnClickListener;
i have this code package com.tct.soundTouch; import android.app.Activity; import android.media.MediaPlayer; import android.os.Bundle; import android.view.MotionEvent;
package com.ebonybutler.cexample3; import android.app.Activity; import android.content.Intent; import android.content.pm.ActivityInfo; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener;
package com.Project_recording; import java.io.File; import java.io.IOException; import android.app.Activity; import android.media.MediaPlayer; import android.media.MediaRecorder; import android.os.Bundle;
package com.android.SMStest; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.telephony.SmsManager; import android.view.View; import android.widget.Button;
debug.java package com.himanshu; import android.app.Activity; import android.os.Bundle; import android.util.Log; public class BookShelfDebugActivity extends Activity

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.