简体   繁体   English

安卓导航组件

[英]Android navigation component

android navigation component reload fragment , and observe mutable data again when navigate back from fragment to fragment , I tried single observe listener but nothing happen keep reloading and hitting api again android导航组件重新加载片段,并在从片段导航回片段时再次观察可变数据,我尝试了单个观察监听器但没有任何反应继续重新加载并再次点击api

public class TermsFragment extends Fragment  {

private TermsViewModel termsViewModel;
private FragmentTermsBinding binding;

public View onCreateView(@NonNull LayoutInflater inflater,
                         ViewGroup container, Bundle savedInstanceState) {
    termsViewModel = new ViewModelProvider(this).get(TermsViewModel.class);
    termsViewModel.init(getContext());
    binding = FragmentTermsBinding.inflate(inflater, container, false);
    termsViewModel.terms.observe(getViewLifecycleOwner(), terms -> {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            binding.terms.setText(Html.fromHtml(terms.replaceAll("<img.+?>", ""), Html.FROM_HTML_MODE_COMPACT));
        } else {
            binding.terms.setText(Html.fromHtml(terms.replaceAll("<img.+?>", "")));
        }

    });

    View root = binding.getRoot();
    return root;
}

@Override
public void onDestroyView() {
    super.onDestroyView();
    binding = null;
  }
}

and my viewmodel Class和我的视图模型类

public class TermsViewModel extends ViewModel implements Onresponse {

public MutableLiveData<String> terms;

Context context=null;

public TermsViewModel() {
    terms = new MutableLiveData<>();

}

public  void  init(Context context){
    this.context=context;
    Map<String,Object> body=new HashMap<>();
    body.put("st", Api.app_data);
    body.put("id",1);
    body.put("lg",Constant.langstr);
    Constant.connect(context,Api.app_data,body,this::onResponse,false);
}

@Override
public void onResponse(String st, JSONObject object, int online) {
    if(object!=null) {
        try {
            terms.setValue(object.getString("data"));
        } catch (JSONException e) {
            e.printStackTrace();
        }

     }
  }

}

When you navigate back and forth the view does not survive but the fragment does .当您来回导航时,视图不会存在,片段会存在 Thus you can move any initialization code to onCreate instead of onCreateView .因此,您可以将任何初始化代码移动到onCreate而不是onCreateView onCreate is called once per fragment's lifetime. onCreate每个片段的生命周期调用一次。

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
 
    termsViewModel = new ViewModelProvider(this).get(TermsViewModel.class);
    termsViewModel.init(getContext());
}

This way you will:这样你将:

  • create view model only once!只创建一次视图模型! It is crucial as replacing view model with a new instance discards all the data you have previously loaded;使用新实例替换视图模型会丢弃您之前加载的所有数据,这一点至关重要;
  • call init only once in a lifetime of this fragment.在此片段的生命周期中仅调用一次init

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

相关问题 带导航视图的导航组件 android - navigation component with navigation view android 片段未显示在 Android 导航组件中 - Fragments are not displaying in Android Navigation component 具有从 Drawerlayout 过渡的 Android 导航组件 - Android Navigation Component with Transition from Drawerlayout Android导航组件不断重新加载WebView - Android Navigation Component keeps reloading WebView Android 导航组件 - 为什么要在导航图中添加参数? - Android Navigation Component - Why add arguments in the navigation graph? Android 导航组件 - 如果用户未登录,则导航到其他活动 - Android Navigation Component - Navigation to additional activity if user not logged in Android导航组件侧导航仅在特定片段上 - Android navigation component side navigation just on specific fragment 使用导航组件在 Android 中启动 Play Store 应用 - Start Play Store app in Android using navigation component Android Jetpack 导航组件太慢。 有什么问题? - Android Jetpack Navigation Component too slow. What's the problem? Android 导航架构组件:如何将捆绑数据传递给 startDestination - Android Navigation Architecture Component: How to pass bundle data to startDestination
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM