Reputation: 75
saveInstanceState()
doesn't work as expected.
I'm trying to read the data so I don't have to do the query again when a user reselects the tab. The fragment is a tab in a actionbar. Everything works properly, I just can't get savedInstanceState to be anything else then NULL, anyone has any idea?
This is my code:
public class SpecsFragment extends Fragment {
private String mText;
private ArrayList<HashMap> result;
private Converter c;
private View fragView;
public SpecsFragment() {
mText = "specs";
c = new Converter();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
fragView = inflater.inflate(R.layout.product_info_fragment, container, false);
return fragView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if(savedInstanceState == null) {
Bundle args = getArguments();
int id = Integer.parseInt(args.getString("id"));
String query = "SELECT * FROM products WHERE id = " + id;
try {
ZoekQueryTask zoekQueryTask = new ZoekQueryTask(query);
result = zoekQueryTask.getResults();
}
catch (Exception e) {
Log.e("Afgevangen error", e.getMessage());
}
}
else {
result = new ArrayList();
result.add((c.BundleToHashMap(savedInstanceState)));
}
TextView text = (TextView) fragView.findViewById(R.id.textView1);
text.setText(mText);
text.append("\n\n");
text.append(result.get(0).get("ean").toString());
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
savedInstanceState = c.HashmapToBundle(result.get(0));
}
}
Using
savedInstance.putAll(c.HashmapToBundle(result.get(0)));
Still does not work.
savedInstance.putBundle("results", c.HashmapToBundle(result.get(0)));
Doesn't work either, anyone have any idea?
I solved the problem by working around it, instead of trying to store savedInstanceState
. I wrote functions to get and set a Bundle from the fragments parent activity, and then get the activity by using:
YourActivity parent = (YourActivity) this.getactivity();
And just call the function for getting and setting a bundle which you write yourself.
Upvotes: 0
Views: 2163
Reputation: 75
I solved the problem by working around it, instead of trying to store savedInstanceState. I wrote functions to get and set a Bundle from the fragments parent activity, and then get the activity by using:
YourActivity parent = (YourActivity) this.getactivity();
And just call the function for getting and setting a bundle which you write yourself
Upvotes: 1
Reputation: 3658
If your Fragment is defined in an XML layout, ensure that you have specified an android:id!
Upvotes: 0