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 7616249
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:50:43+00:00 2026-05-31T02:50:43+00:00

I have an application which uses a SQL database. This is encapsulated by a

  • 0

I have an application which uses a SQL database. This is encapsulated by a SQLiteOpenHelper class. When the splash screen launches, it calls init on a DataProvider class which stores a protected static instance of the SQLiteOpenHelper. init simply calls the constructor of the SQLiteOpenHelper:

public class UKMPGData extends SQLiteOpenHelper
{
public UKMPGData(Context context, String databaseName)
{
    super(context, databaseName, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db)
{
    //create table and set up triggers etc
}
    @Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
    onCreate(db);
}
}

public class UKMPGDataProvider
{
protected static UKMPGData uKMpgData;

public static void init(Context aApplicationContext, String aDatabaseName)
{       
    uKMpgData = new UKMPGData(applicationContext, databaseName);
}

public static void close()
{
    uKMpgData.close();
}
}

I then had two further classes which extended UKMPGDataProvider and therefore had access to uKMpgData. These classes retrieve and store particular types of data from the database. Eg.

public class VehicleDataProvider extends UKMPGDataProvider
{

    public static Cursor getVehicles()
{
    Cursor cursor = null;

    SQLiteDatabase db = uKMpgData.getReadableDatabase();
    cursor = db.query(VEHICLE_TABLE_NAME, GET_VEHICLES_FROM_CLAUSE, null, null, null, null, ORDER_BY);

    return cursor;
}
//...
}

This all seemed to work fine until I noticed that if the application was running and then forced to the background, if it was left for a number of hours, when the app was brought back to the foreground I would get a null pointer in an Activity class which called getVehicles() (see above). It turns out, uKMpgData was no longer referencing an object.

I understand that Android can kill processes when it is deemed necessary, but don’t understand what happened to my app to get the null pointer – if my app’s process was killed, then wouldn’t a new instance of the app be launched? In other words, a new SplashScreen would initialise the database object and therefore no null pointer exception.

I must be missing something – what state was my app in to have memory reclaimed (the reference to the database object) but to display the last visible activity upon relaunch.

Incidentally, the bug is now fixed. VehicleDataProvider and the other similar class no longer extend the superclass data provider (UKMPGDataProvider), which now holds a private reference to ukMpgData. All methods which touch the database now go through UKMPGDataProvider which will check for null and reinitialise if necessary.

Thanks in advance,
Barry

  • 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-31T02:50:44+00:00Added an answer on May 31, 2026 at 2:50 am

    When Android kills your application to reclaim memory, your Application/Activity objects are all unloaded and will no longer exist. However, Android saves certain information so that your application can be restored to that approximate state when you try to run it again.

    Part of this includes information about the state of the Activity stack (i.e. which Activities were running) before it was destroyed. As you noticed, Android restores your application to the Activity that was last running, and not the beginning of your app (the splash screen). This is a ‘feature’, and I can only assume it is an attempt to provide a seamless experience for the user who should not even notice that the app was unloaded.

    There is no reason why Android should recreate your splash screen (even if it still existed in the Activity stack) before recreating the current Activity. That kind of dependency is not encouraged, and is not something you can rely on as Android loads/unloads Activities as it sees fit. In general, having static state that is initialised in previous activities is a sure way to run into problems in this situation. Since the splash screen was not recreated, it won’t have initialiased the UKMPGDataProvider before you are using it in another Activity, hence the NullPointerException.

    You can solve this in two ways.

    1. Initialise the UKMPGDataProvider inside Activity.onCreate(Bundle) of each Activity that uses it. If one of these Activities are being restored, it is guaranteed to get a callback to onCreate and hence the data provider will always be initialised.

    2. Initialise the UKMPGDataProvider inside Application.onCreate(). This is guaranteed to be called before any Activity is started, even in the even of memory reclaimation/restoration. If you do not yet have a custom Application class, you should create a subclass and then point to it in your AndroidManifest.xml for it to be used.

    As an aside, implementing Activity.onSaveInstanceState(Bundle) is one way to save additional state relevant to your application, which will be given back to you in Activity.onCreate(Bundle) (it is the Bundle argument) upon restoration. It is meant for transient state only, such as some view state that arose after some user actions (in particular the state of custom views). It is not suitable for this case, since you can generate a new UKMPGDataProvider easily and it does not have any state linked to the previous session.

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

Sidebar

Related Questions

So I have this VB.Net Application. It uses SQL Server database, for which it
I have created a windows form application which uses an SQL database. The database
We have an application which uses an instance of Sql Server locally for its
I have created my first project in C# which uses sql server database (file
We have an WCF application which uses NHibernate to query data from the database.
I have a winform C# application which uses sql server 2005 express. I am
I have a C# WPF desktop application which uses SQL Compact 3.5 as its
Here's my problem: I have an application which uses sql server express 2005, and
I am trying to deploy an application I have created which uses a SQL
I have one application which uses NHibernate to save entities to a database, and

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.