Saved State getting nullified

36 views Asked by At

I am trying to fetch details from savedInstanceStatethat were saved in a newInstace() and needs to be fetched in onCreateView().

The problem:- When I try to fetch the details, It throws an excpetion.

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference

I am unable to identify why is savedInstanceState getting nullified or GCed.

Defected Code:-

public class FeedDetailsFragment extends Fragment {


    public static final String TAG = FeedDetailsFragment.class.getSimpleName();
    private static String KEY_NAME="1";
    private static String KEY_TEXT="2";
    private static String KEY_IMAGEURL="3";
    private static String KEY_TITLE="4";
    private static String KEY_TIME="5";
    private static String KEY_DESC="6";

    public static FeedDetailsFragment newInstance(DataModel data){
        FeedDetailsFragment detailsFragment=new FeedDetailsFragment();
        Log.d(TAG, "Is Data Null? " + ((data == null) ? "Yes" : "No") + " For example: name is " + data.getName());
        Bundle bundle=new Bundle();
        bundle.putString(KEY_NAME,data.getName());
        bundle.putString(KEY_TEXT,data.getText());
        bundle.putString(KEY_IMAGEURL,data.getImageUrl());
        bundle.putString(KEY_TITLE,data.getTitle());
        bundle.putLong(KEY_TIME,data.getTime());
        bundle.putString(KEY_DESC,data.getDescription());
        detailsFragment.setArguments(bundle);
        fragtag.put(TAG,detailsFragment);
        return detailsFragment;
    }
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle state) {
        View view = inflater.inflate(R.layout.fragment_feeddetail, container, false);
        TextView detail_title=view.findViewById(R.id.detail_title);
        TextView detail_text=view.findViewById(R.id.detail_text);
        ImageView detail_image=view.findViewById(R.id.detail_image);
        TextView detail_name=view.findViewById(R.id.detail_name);
        TextView detail_desc=view.findViewById(R.id.detail_desc);
        Log.d(TAG, "Is State Null? " + ((state == null) ? "Yes" : "No"));
            detail_title.setText(state.getString(KEY_TITLE));
            detail_text.setText(state.getString(KEY_TEXT));
            requestImage(detail_image,state.getString(KEY_IMAGEURL));
            detail_name.setText(state.getString(KEY_NAME));
            detail_desc.setText(state.getString(KEY_DESC));
        return view;
    }
}

LogCat Output: this is crucial. You may want to have a look at this.

09-07 15:53:38.374 14729-14729/io.github.abhirojp.myfeed_android D/MainActivity: Display Details
09-07 15:53:38.379 14729-14729/io.github.abhirojp.myfeed_android D/FeedDetailsFragment: Is Data Null? No For example: name is John Zeratsky
09-07 15:53:38.399 14729-14729/io.github.abhirojp.myfeed_android D/FeedDetailsFragment: Is State Null? Yes
09-07 15:53:38.399 14729-14729/io.github.abhirojp.myfeed_android D/AndroidRuntime: Shutting down VM
1

There are 1 answers

1
Vladyslav Matviienko On BEST ANSWER

Bundle state is not used to pass initial data to the fragment, but to restore fragment's state after it has been destroyed and re-created.

Since you are using detailsFragment.setArguments(bundle); to set Bundle into Fragment, you should use this.getArguments() to get this data inside a fragment.

One more option is to simply use setters for the values you need.