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.