如何在我的片段上保存项目的状态?

How save the state of the item on my fragment?

我从 Url 和 AsyncTask 得到一个 ArrayList,然后插入 Fragment 上的 ListView,但每次我更改片段我必须再次从 Url 获取 json 数据,所以我尝试使用 onSaveInstanceState() 将我的 ArrayList 保存为 String 并转换为我的 ArrayList 使用 JsonObjectGson Library 但我无法保存数据。

OnSave 方法

      @Override
            public void onSaveInstanceState(@NonNull Bundle outState) {
            outState.putString("cursos", this.cursosString);

            super.onSaveInstanceState(outState);
      }

OnCreateView 方法

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    View view = inflater.inflate(R.layout.fragment_cursos, container, false);
    final ListView lvCursos = view.findViewById(R.id.lvCursos);

    if(savedInstanceState != null){

        Type arrayListCurso = new TypeToken<ArrayList<Curso>>(){}.getType();
        ArrayList<Curso> cursos = new Gson().fromJson(savedInstanceState.getString("cursos"), arrayListCurso);
        ListaCursosAdapter adapter = new ListaCursosAdapter(cursos, getActivity());
        lvCursos.setAdapter(adapter);

    } else {
    ...

更改片段方法

   public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {

    switch (menuItem.getItemId()){

        case R.id.navigation_profile:
            getSupportFragmentManager()
                    .beginTransaction()
                    .replace(R.id.nav_fragment, perfilFragment)
                    .commit();
            return true;

这是解决方案,您只需决定如何实施getTagForFragmentId()getFragmentForFragmentTag(fragmentTag),这应该很简单。

下面代码的想法是 .hide() 呈现之前的片段,然后 .add(fragment) 一个新片段,如果它以前没有呈现,或者 .show() 片段,如果它已经在后台。

String lastPresentedFragmentTag = "";

public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
    switch (menuItem.getItemId()){
        case R.id.navigation_profile:
            // getTagForFragmentId() can be just a switch case function
            // or you can hardcode value instead
            String fragmentTag = getTagForFragmentId(R.id.navigation_profile);
            presentFragment(fragmentTag);
            return true;
        //...


public void presentFragment(String fragmentTag) {
    if (lastPresentedFragmentTag == fragmentTag) {
        return;
    }

    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    // Hide previous fragment
    if (lastPresentedFragmentTag != null) {
        Fragment lastFragmenter code hereent = getSupportFragmentManager().findFragmentByTag(lastPresentedFragmentTag);
        ft.hide(lastFragment);
    }

    Fragment currentFragment = getSupportFragmentManager().findFragmentByTag(fragmentTag);
    if (currentFragment == null) {

            // Implement getFragmentForFragmentTag(fragmentTag) it will be another switch case function that will return you fragment
            val fragment = getFragmentForFragmentTag(fragmentTag);
            transaction.add(R.id.fragmentContainer, fragment, tag);
        } else {
            transaction.show(currentFragment);
        }
    }

    transaction.commitNow();
}