【问题标题】:How to refresh the main view?如何刷新主视图?
【发布时间】:2019-04-24 11:25:32
【问题描述】:

我的活动中有 3 个片段,每个片段的编号为 1,2 和 3。这有点像一步活动;如果片段 1 完成,则相关视图内容将被第二个片段替换,对于 frgs 也是如此。 2 和 3。

我可以在我的操作栏中更改应用程序。语言,当我更改它时,我需要使用新资源刷新相关布局,为此我使用:

   private fun refreshView() {
        this.recreate()
   }

我的铅。是重新创建活动并且我在第二个或第三个片段中时,活动返回到第一个。

我应该如何解决这个问题?是否有解决方案可以在不重新创建活动的情况下刷新视图?

【问题讨论】:

    标签: android kotlin


    【解决方案1】:

    尝试创建一个静态常量类或应用程序类,您可以将当前片段保存为计数或字符串,并在重新创建后显示您保存在上述类中的片段。

    【讨论】:

      【解决方案2】:

      您可以将片段编号保存在 SharedPreference 中。但在我看来,重新创建活动是错误的方法。您应该只重新加载当前片段中的所有数据。

      【讨论】:

        【解决方案3】:

        我建议您查看说明如何保存 UI 状态的文档:https://developer.android.com/topic/libraries/architecture/saving-states

        在我看来,ViewModel 与 LiveData 相结合是解决您问题的不错选择。

        ViewModel:https://developer.android.com/topic/libraries/architecture/viewmodel

        实时数据:https://developer.android.com/topic/libraries/architecture/livedata

        【讨论】:

          【解决方案4】:
          public class MainActivity extends AppCompatActivity {
          private int fragmentIndex = 0;
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
          
              Button button = findViewById(R.id.btn_next);
              button.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View view) {
                      fragmentIndex++;
                      if (fragmentIndex >= 3) {
                          fragmentIndex = 0;
                      }
                      showFragment(fragmentIndex);
                  }
              });
              //get the index saved
              if (savedInstanceState != null) {
                  fragmentIndex = savedInstanceState.getInt("index");
              }
              showFragment(fragmentIndex);
          }
          
          //override this function to save fragment index. This function will be called when activity is destroyed.
          //And when activity is recreated, you can get the index from the savedInstanceState as code above.
          @Override
          protected void onSaveInstanceState(Bundle outState) {
              super.onSaveInstanceState(outState);
              outState.putInt("index", fragmentIndex);
          }
          
          
          
          private void showFragment(int index) {
              Fragment fragment = SimpleFragment.createFragment(String.valueOf(index));
              getSupportFragmentManager().beginTransaction().replace(R.id.container, fragment).commit();
          }
          
          public static class SimpleFragment extends Fragment {
              public static SimpleFragment createFragment(String index) {
                  SimpleFragment fragment = new SimpleFragment();
                  Bundle bundle = new Bundle();
                  bundle.putString("index", index);
                  fragment.setArguments(bundle);
                  return fragment;
              }
          
              @Nullable
              @Override
              public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
                  View rootView = inflater.inflate(R.layout.fragment_simple, container, false);
                  TextView indexView = rootView.findViewById(R.id.tv_index);
                  String index = getArguments().getString("index");
                  indexView.setText(index);
                  return rootView;
              }
          }
          

          }

          您可以通过旋转设备来测试我的代码,无论是否覆盖onSaveInstanceState 函数,从而重新创建活动

          【讨论】:

            猜你喜欢
            • 2015-01-26
            • 2014-11-13
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-01-06
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多