Issue
My app has some global/static data structures that need to be initialized before showing the main Activity, so I put the work into onCreate
method of my SplashActivity
, which just shows a splash image for 2 seconds, starts another activity, and finishes itself:
initializeGlobalData();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent i = new Intent(SplashActivity.this, MainActivity.class);
startActivity(i);
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
finish();
}
}, 2000);
Now, my app sometimes mysteriously crashes because of null pointer reference - some global data structures are not initialized. It could only mean that the onCreate method of SplashActivity is not called (right?).
I have no idea how to reproduce this, but it happens quite often. It's possible I left the app in the background, and re-enter. But application level data should not be released, right?
Solution
It's possible I left the app in the background, and re-enter. But application level data should not be released, right?
It depends on what you mean when you say "global/static data structures that need to be initialized".
If the user leaves your app, it is expected that the Android OS might terminate your app's process. When this happens, anything that is stored only in memory will be lost.
A common example is e.g. some public static
value that you load once and then refer to throughout your application. When the OS terminates your app's process, and then the user returns to your app, that public static
value will need to be re-initialized.
Answered By - Ben P.
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.