感谢这个答案Android Fragment lifecycle issue (NullPointerException on onActivityResult)当我在调用 startActivityForResult 后在我的 fragment 中收到 NPE 时,我设法重新创建了一个场景。 所以我有
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, PHOTO_SELECT_REQUEST_CODE);
break;
从我的 fragment 调用,然后我的 Activity 接收到 onPause、onStop 和 onDestroy,因此调用 startActivityForResult 的 fragment 也获得了 onDestroy。在我选择一张图片后,我在我的 Activity 上获得了一个新的 onCreate,然后我在我现在被销毁的原始 fragment 上获得了一个 public void onActivityResult。
我的问题是,由于这可能(尽管很少见)情况,如何恢复传递给它们的整个 fragment 和对象堆栈,以及如何防止原始 fragment 泄漏?
最佳答案
当您的代码执行以下行时:
startActivityForResult(photoPickerIntent, PHOTO_SELECT_REQUEST_CODE);
您的 Activity 每次都会得到 onPause()、onStop() 而不是 onDestroy()。如果 android 系统在那个运行时内存不足,那么你的 Activity 可能会得到 onDestroy()
为了保存 fragment 和 Activity 的状态并防止泄漏:
在 onSaveInstanceState(Bundle) 中保存您的状态
In situations where the system needs more memory it may kill paused processes (after
onPause()) to reclaim resources. Because of this, you should be sure that all of your state is saved by the time you return from this function. In generalonSaveInstanceState(Bundle)is used to save per-instance state in the activity and this method is used to store global persistent data (in content providers, files, etc.)
在 onRestoreInstanceState(Bundle) 中恢复你的状态
onRestoreInstanceState(Bundle)method is called afteronStart()when the activity is being re-initialized from a previously saved state, given here insavedInstanceState. Most implementations will simply useonCreate(Bundle)to restore their state, but it is sometimes convenient to do it here after all of the initialization has been done or to allow subclasses to decide whether to use your default implementation. The default implementation of this method performs a restore of any view state that had previously been frozen byonSaveInstanceState(Bundle).
这可以帮助您实现您的要求
关于Android Fragment 被销毁但收到 onActivityResult,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29465182/
简而言之错误:NOTE:Gem::SourceIndex#add_specisdeprecated,useSpecification.add_spec.Itwillberemovedonorafter2011-11-01.Gem::SourceIndex#add_speccalledfrom/opt/local/lib/ruby/site_ruby/1.8/rubygems/source_index.rb:91./opt/local/lib/ruby/gems/1.8/gems/rails-2.3.8/lib/rails/gem_dependency.rb:275:in`==':und
有人知道在发布新版本的Ruby和Rails时收到电子邮件的方法吗?他们有邮件列表,RubyonRails有一个推特,但我不想听到那些随之而来的喧嚣,我只想知道什么时候发布新版本,尤其是那些有安全修复的版本。 最佳答案 从therailsblog获取提要.http://weblog.rubyonrails.org/feed/atom.xml 关于ruby-on-rails-如何在发布新的Ruby或Rails版本时收到通知?,我们在StackOverflow上找到一个类似的问题:
我刚刚更新了我的gem,当我尝试运行Guard时,出现以下错误:Guard::RSpecDEPRECATIONWARNING:The:clioptionisdeprecated.Pleasecustomizethenew:cmdoptiontofityourneed.这是我的Guard文件:guard'rspec',cli:'--drb'dowatch(%r{^spec/.+_spec\.rb$})watch(%r{^lib/(.+)\.rb$}){|m|"spec/lib/#{m[1]}_spec.rb"}watch('spec/spec_helper.rb'){"spec"}#Ra
在我的ramaze应用程序上显示£符号时,我收到“不兼容的字符编码:CP850和UTF-8”。我怎样才能摆脱这个错误?我的head标签中有UTF-8元标签。当我用键盘输入£符号时会发生这种情况。看。我已将以下代码放入我的ruby文件中,但没有解决问题。#encoding:UTF-8Encoding.default_external='utf-8'Encoding.default_internal=Encoding::UTF_8 最佳答案 尝试强制编码以查看是否可以解决问题:your_string.force_encoding(:
在我的应用程序中,有些资源无法销毁。所以我这样写了我的模型:before_destroy:destroy_checkdefdestroy_checkifsome_reason?errors.add(:base,'cannotdestroythisresource!')enderrors.blank?end然后,当我在ActiveAdmin中单击销毁按钮时,没有任何显示:没有错误,没有消息,并且记录没有真正销毁。如何在销毁失败时显示错误消息? 最佳答案 首先使用模型的before_destroy回调来检查记录是否可以被销毁(这里如果学
我尝试重现http://railscasts.com/episodes/346-wizard-forms-with-wicked轨道广播。我尝试通过四步向导创建报告。我生成Controllerreport_steps将resources:report_steps添加到routes.rb创建步骤View现在我尝试调用它(就像在railscats中通过在浏览器的地址栏中键入localhost:3000/report_steps/step1一样)并接收:RoutingErroruninitializedconstantReportStepsController::Wicked问题是什么?我使
我尝试运行gempristine--all并且我不断在控制台中获取它。当我运行gempristine--all我得到这个:权限被拒绝....我什至尝试为每个gem执行geminstall并且仍然得到这个:`Ignoringbinding_of_caller-0.7.2becauseitsextensionsarenotbuilt.Try:gempristinebinding_of_caller--version0.7.2Ignoringbyebug-5.0.0becauseitsextensionsarenotbuilt.Try:gempristinebyebug--version5.
我有一个场景,我需要极快地分发和处理作业。我将在队列中快速填充大约45个作业,我可以同时处理大约20个(5台机器,每台机器4个内核)。每项工作花费的时间长短不一,而且垃圾收集使事情变得复杂,因此我需要能够让消费者离线以进行垃圾收集。目前,我的一切都与pop一起工作(每个消费者每5毫秒弹出一次)。这似乎是不可取的,因为它转换为每秒600个pop请求到rabbitmq。如果有一个类似于订阅的pop命令,但只针对一条消息,我会很高兴。(进程会阻塞,等待来自rabbitMQ连接的输入,通过类似于Kernel.select的东西)我试图欺骗AMQPgem做这样的事情,但它不起作用:我似乎无法取消
我刚刚创建了一个新的空Rails应用程序,它几乎是空的。我创建它只是为了找出问题所在,但我收到了以下错误。我正在使用Rails3.1.0和Ruby1.9.2Errormessage:Couldnotfindrake-0.9.2.2inanyofthesources(Bundler::GemNotFound)Exceptionclass:PhusionPassenger::UnknownError所以我将rake-0.9.2.2添加到Gemfile并运行bundle,但我仍然收到相同的错误消息。我不知道问题出在哪里,我什至尝试打开Rails日志文件,但里面什么也没有。我尝试了很多次以不同
所以我在Rails上工作,对我的用户模型感到有点沮丧,所以我销毁了它(我也在使用设计,这可能是错误的来源)。在我的辩护中,我感到疲倦、沮丧,而且有点微醺。我尝试使用简单的railsgscaffolduserfirst:textlast:text将其添加回去,但在尝试生成模型时出现以下错误。我该如何修复这一切备份?/home/action/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.0.0/lib/active_support/inflector/methods.rb:226:in`const_get':uninitializedcons