Skip to content Skip to sidebar Skip to footer

Getting Null Pointer Exception On When Coming Back To Activity From Another Activity

I have a problem with saving state and restoring it. I have two variables that I want to save when another activity is started and restore them when I press home back button. There

Solution 1:

@OverrideprotectedvoidonSaveInstanceState(Bundle outState)
{
    outState.putString("CATEGORY_ID", state_category_id);
    outState.putString("CATEGORY_NAME", state_category_name);

    super.onSaveInstanceState(outState);
}

And Retrieve data after getting back from different activity :

@OverrideprotectedvoidonRestoreInstanceState(Bundle savedInstanceState) {

    if (savedInstanceState != null) {
        if (savedInstanceState.containsKey("CATEGORY_ID")) {
            cat_id = extras.getString("CATEGORY_ID"); 
        }

       if (savedInstanceState.containsKey("CATEGORY_NAME")) {
           cat_name = extras.getString("CATEGORY_NAME");
       }

    }

    super.onRestoreInstanceState(savedInstanceState);
}

Solution 2:

put this condition in else part

if(getIntent().hasExtra("CATEGORY_ID"))
    cat_id = extras.getString("CATEGORY_ID");
else
    cat_id=//assign default idif(getIntent().hasExtra("CATEGORY_NAME"))
    cat_name = extras.getString("CATEGORY_NAME");
else
    cat_name=//assign default name

Solution 3:

Thanks everyone for your support.

Actually I solved problem by storing needed data in static variables and getting them when bundle is null.

Thanks again.

Post a Comment for "Getting Null Pointer Exception On When Coming Back To Activity From Another Activity"