本文介绍了片段onStop()与onDetach(),onDestroy()或onDestroyView()相同吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是代表Android中的新开发人员应该提出的问题.

This is the question worth to be asked on the behalf of the new developers in Android.

想法是要深入了解为何如此编写框架.此外,开发人员还面临着悬而未决的指针,非法状态和此类运行时崩溃的情况,并且他们不知道为什么会发生这种情况.

Idea is to provide a deep understanding to why the framework is written as such.Also, developers face dangling pointers, illegal states and such run-time crashes, and they do not have a clue that why it is happening.

现在,程序员每天都大量使用回调和工厂模式.委托类对象的使用减少了对Singleton类的需要,并减少了诸如C,C ++之类的语言中的多重继承.

Programmers now a days heavily use call back and factory patterns. Use of delegate class objects reduce the need for Singleton classes, and the need of multiple inheritance in languages like C, C++.

当开发人员了解组件之间基于Handler的消息传递时,他们会感到非常兴奋.

Developers thrill when they come to understand Handler based message passing between components.

这些方法中的哪一种更可靠,因为知道Fragment的上下文不应再由其组件使用,或者不再作为其父项的Activity使用:

Which one of these methods is more reliable to know that the context of the Fragment, should no longer be used by its components, or outside from the Activity that is parenting it:

  1. onStop()
  2. onDetach()
  3. onDestroyView()
  4. onDestroy()

最好的问候.

推荐答案

我正在跟踪所有这些方法,以停止使用片段的getView().我从逻辑上认为onDestroy()是最适合此目的的方法.

I am tracking all of these methods, to stop using getView() of fragment. I logically feel onDestroy(), is the most suitable method for this purpose.

在此答案中,我以相同的方式使用跟踪器: https://stackoverflow.com/a/52017405/787399

I am using the trackers in the same way in this answer:https://stackoverflow.com/a/52017405/787399

此继承策略极大地帮助和改善了ActivityFragments生命周期的含义.我实际上已经非常强大,可以拥有未隐式提供的那些功能:就像您可以处理系统后退(在onBackPressed()方法中在BaseActivity中管理的)只要在Fragment上按后退一样,并且您可以阻止back pressed event调用,直到满足某些条件为止,或者放置OK_Cancel confirmation alert表示是否要真正退出当前片段.

This inheritance strategy greatly helps and improves the meaning of the Activity and Fragments life-cycles. I fact it is so power full that you can have those features that are not implicitly provided: Like you can handle system back press (managed in the BaseActivity in onBackPressed() method) whenever back is pressed on the Fragment, and you can block the back pressed event call, till some condition is met, or place an OK_Cancel confirmation alert, that whether you want to really exit the current fragment.

快乐编码:-)

这篇关于片段onStop()与onDetach(),onDestroy()或onDestroyView()相同吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 21:38