简体   繁体   中英

Saving fragmentstate with savedInstanceState

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.

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

如果您的片段是在XML布局中定义的,请确保已指定android:id!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM