0

There is a Fragment in my android project in which it gets data dynamically from another class.I save that data(one string and Int) into bundle and And I want that bundle to be restored when screen rotates. So I have used onSaveInstanceState method.

In this fragment, "respond" method get data(one string and Int) from another class. I can print those strings in Logcat in respond method.

Fragment Code:

     public class Images extends android.support.v4.app.Fragment implements Imageinfo {

private RecyclerView IRecyclerView;
private RecyclerView.Adapter IAdapter;
private RecyclerView.LayoutManager ILayoutManager;

private Context ctx;

private Bundle bundle=new Bundle();

     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View v =inflater.inflate(R.layout.images, container, false);

    Log.d("ONCREATE VIEW", "TRUE");


    IRecyclerView = (RecyclerView) v.findViewById(R.id.images_tab);
    IRecyclerView.setHasFixedSize(true);
    ILayoutManager = new LinearLayoutManager(getContext());
    IRecyclerView.setLayoutManager(ILayoutManager);

    if(savedInstanceState!=null){
        Log.d("BUNDLE ADDED NOT NULL", String.valueOf(savedInstanceState.size()));
        IAdapter = new ImageAdapter(this.getContext(),((fileselect)getContext()).imageset,bundle);
        IRecyclerView.setAdapter(IAdapter);
    }

    else{
        Log.d("BUNDLE ADDED NULL", "TRUE");
        IAdapter = new ImageAdapter(this.getContext(),((fileselect)getContext()).imageset,null);
        IRecyclerView.setAdapter(IAdapter);
    }

    return v;

}




@Override
public void onSaveInstanceState(Bundle outState) {
    outState=bundle;
    Log.d("SAVING TIME", String.valueOf(outState.size()));
    super.onSaveInstanceState(outState);


}
@Override
public void respond(String str,int type) {


    if(str!=null) {
        if (type == 1) {
            bundle.putString(str, str);
            Log.d("BUNDLE ADDED", bundle.getString(str));
            Log.d("BUNDLE ADDED Size",String.valueOf(bundle.size()));
        } else {

            bundle.remove(str);
            Log.d("BUNDLE REMOVED Size", String.valueOf(bundle.size()));

        }
    }
}
    }

PROBLEM:

Although the method respond receiving data(one string and Int) and saving into Bundle, bundle is becoming size zero in onSaveInstanceState when screen rotated. onSaveInstanceState is getting called whenever i rotate screen but the bundle is becoming size zero. As bundle is becoming size zero, I could not restore the two strings.

sandesh
  • 390
  • 6
  • 20

1 Answers1

1

OK the problem is that, instead of adding content to the outstate variable, you are trying to reference a local variable.

The point is that the instance of the Images class is destroyed on rotation, and so will your bundle variable.

The correct way to achieve what you are trying to do is to add your strings to the outstate variable in the onSaveInstanceState method, and read it from the savedInstanceState in the onCreateView.

Try this out:

@Override
public void onSaveInstanceState(Bundle outState) {
    outState.putString("YOUR_STRING_NAME", bundle.getString("YOUR_STRING_NAME"));
    Log.d("SAVING TIME", String.valueOf(outState.size()));
    super.onSaveInstanceState(outState);
}
malrok44
  • 592
  • 6
  • 17
  • But the onSaveInstanceState gets called just before destroying right? If it gets called before destroying then bundle should not be zero as it has not destroyed yet. So then why can't I just write outstate = bundle ? – sandesh Apr 24 '16 at 10:26
  • At best, you will have a dead reference to an exiting fragment which will be kept beacuse one of its variable was not destroyed, but this cannot work as the FragmentManager class from Android will create a new instance of your Images class. This new reference will host a new bundle variable with nothing in it, which will be used in the onCreateView to try and restore the data. At this point, the bundle data is a brand new variable, and you cannot expect it to have the data stored in your previous Images instance. – malrok44 Apr 24 '16 at 10:29
  • It is working if we put strings into bundle in onSaveInstanceState. But I don't need that. What I need is I want to update the string values received in respond method into "outstate" bundle in onSaveInstanceState. Is there anyway I can do that? – sandesh Apr 24 '16 at 10:34