简体   繁体   English

用savedInstanceState保存fragmentstate

[英]Saving fragmentstate with savedInstanceState

saveInstanceState() doesn't work as expected. saveInstanceState()不能按预期方式工作。
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? 一切正常,我只是无法将saveInstanceState设置为NULL,否则有人知道吗?

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 . 我通过解决此问题解决了这个问题 ,而不是尝试存储savedInstanceState I wrote functions to get and set a Bundle from the fragments parent activity, and then get the activity by using: 我编写了从片段父活动中获取并设置Bundle的函数,然后使用以下方法获取该活动:

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. 通过解决它来解决了问题,而不是尝试存储saveInstanceState。 I wrote functions to get and set a Bundle from the fragments parent activity, and then get the activity by using: 我编写了从片段父活动中获取并设置Bundle的函数,然后使用以下方法获取该活动:

YourActivity parent = (YourActivity) this.getactivity(); 

And just call the function for getting and setting a bundle which you write yourself 只需调用该函数即可获取和设置您自己编写的捆绑软件

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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