Note that there are some explanatory texts on larger screens.

plurals
  1. POSaving fragmentstate with savedInstanceState
    primarykey
    data
    text
    <p><code>saveInstanceState()</code> doesn't work as expected.<br> 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? </p> <p>This is my code:</p> <pre><code>public class SpecsFragment extends Fragment { private String mText; private ArrayList&lt;HashMap&gt; 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)); } } </code></pre> <p>Using </p> <pre><code>savedInstance.putAll(c.HashmapToBundle(result.get(0))); </code></pre> <p>Still does not work.</p> <pre><code>savedInstance.putBundle("results", c.HashmapToBundle(result.get(0))); </code></pre> <p>Doesn't work either, anyone have any idea?</p> <p><strong>I solved the problem</strong> by working around it, instead of trying to store <code>savedInstanceState</code>. I wrote functions to get and set a Bundle from the fragments parent activity, and then get the activity by using: </p> <pre><code>YourActivity parent = (YourActivity) this.getactivity(); </code></pre> <p>And just call the function for getting and setting a bundle which you write yourself.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload