Pete
Pete

Reputation: 51

My app crashes after being paused for along time and after onRestoreInstanceState

Ok guys I really need some help here I have a published app Auto Inspection and on my Droid 4 in real world usage pressing the physical home button on my phone pausing the app for along time, it crashes when after onRestoreInstanceState is called. The most I can figure out is the fragment that I'm returning to has a button "Add New Vehicle" that updates the next fragment when clicked as soon as that button is clicked it crashes. I think it because the fragment state of the third fragment is lost. The app always works perfect until it's paused for some period of time. Here is my stack trace:

    java.lang.NullPointerException
at pete01507.AutoInspection.af.a(Unknown Source)
at pete01507.AutoInspection.MainActivity.a(Unknown Source)
at pete01507.AutoInspection.ae.onClick(Unknown Source)
at android.view.View.performClick(View.java:4106)
at android.view.View$PerformClick.run(View.java:17052)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5059)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
at dalvik.system.NativeStart.main(Native Method)

Any help would be appreciated. Thanks

Here is the onRestoreInstanceState:

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    Log.d(TAG, "onRestoreInstanceState");
    if (savedInstanceState == null) {
        Log.d(TAG, "Restore onSaveInstanceState was empty");
        car="";
        fresh=false;
    }else{
        Log.d(TAG, "Restore onSaveInstanceState else was not empty");
        car = (String) savedInstanceState.get("car");
        load();
        mViewPager.setCurrentItem(savedInstanceState.getInt("vp"));
    }
    }

Here is the onSaveInstanceState:

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);
    Log.d(TAG, "onSaveInstanceState");
    if (car.equals("")){
        Log.d(TAG, "onSaveInstanceState car not loaded");
    } else {
        Log.d(TAG, "onSaveInstanceState car is loaded");
        savedInstanceState.putString("car", car);
        savedInstanceState.putInt("vp", mViewPager.getCurrentItem());
    }
    }

Update: I have a MainActivity with seven static fragments on a ViewPager the activity doesn't have any views or pages so when the app starts the first page is the first fragment. I removed all calls to the callback interface to update the third fragment when the "+ New Vehicle" button is clicked this button is in the second fragment. Now the app works without crashing but there's no update to fragment #3. The callback interfaces will still update the first fragment but not the third for some reason which I haven't figured out yet is after the app is killed by Android running low on memory and you go click the app again to use it the third and rest of the fragments are no longer accessible via the interfaces. So how would I go about reinstating the fragments like when the app starts when you first install it or this app has an exit button that calls context.finish after the exit button is pressed the interfaces work again?

Upvotes: 2

Views: 1037

Answers (1)

hocikto
hocikto

Reputation: 981

Too bad I can't comment because I am not sure about this, but! I remember that I had similar problem and this sounds familiar to me. So I opened my 2 or 3 years old source code of one app I made. And there was a comment that I wrote.

If app is put into background by home button, and Android is getting out of RAM, it kills the app but saves the activity stack and instance state to bundle. When the app is opened again, it won't launch the launcher activity, but the last one that was on activity stack, and values that were passed to it from other activity are lost.

So, aren't you expecting some values to not be null in onCreate or somewhere else calling getIntent(), in one of your activities?

Not sure about this but I know how frustrating can this be so I wish you luck to find the bug.

Upvotes: 1

Related Questions