我的应用程序在运行自定义版本的 Android Gingerbread 2.3.7 的特殊设备上运行
在某些情况下,系统会终止我的应用程序。我假设设备制造商考虑了这些紧急情况,所有第三方应用程序都应立即关闭,以便设备可以执行其主要任务。
我可以使用模拟器复制我在设备上看到的行为,并在 DDMS 中选择我的任务并单击“停止进程”按钮。这是我看到的行为。
我的应用程序通常会逐步执行四个 Activity , Activity A 启动 Activity B, Activity B 启动 Activity C,而 Activity C 启动 Activity D。因此,当 Activity D 在顶部运行时,我的堆栈是:
A - B - C - D
如果此时进程终止,Activity D 不会收到 onPause() 或 onStop() 调用。它没有机会保存其状态。
进程终止后,Android 的 ActivityManager 为我的应用程序启动一个新任务并启动 Activity C。我认为这是重启崩溃应用程序的标准 Android 行为。
我的问题是我可以控制这种重启行为吗?如果 Android 要重启我的应用程序,我需要恢复 Activity 堆栈,Activity C 单独运行没有意义(单击后退按钮将退出应用程序,这对于此 Activity 没有意义)。
我可以阻止这次重启吗? 我可以让重启按顺序开始我的所有 Activity 吗? 我可以让重启只启动 Activity A 吗?
我确实找到了这个 interesting discussion我相信这解释了为什么 Activity C 被重启而不是 Activity D。
As far as when an activity is restarted -- if the process running the foreground activity goes away, the system will throw away that activity if it does not have a valid saved state for it (typically meaning it is paused and has given the system the result of onSaveInstanceState from before the pause). Once it has decided whether or not to throw away that activity, it will resume whatever activity is now at the top of the stack. If this is one of your activities -- either because you have another behind the one that crashed, or the one that crashed was somehow it the settled pause state -- then it will start your process again to show that top activity.
还有一些类似的问题,例如 Prevent Activity Stack from being Restored?这个有趣的 thread
最佳答案
经过大量试验后,我选择了“Android 开发者 › Activity restart crash after OS kills it”中暗示的方法。有一个问答交流:
问题:
"have stepped through the code and have seen what you are talking about. It calls onSaveInstanceState and saves the data in the Bundle. Will this Bundle information be available when the activity restarts after the OS kills the process?"
回答:
"It's supposed to. You will get that Bundle in onCreate() and in onRestoreInstanceState(). onCreate() can be passed null for the Bundle, if there is no Bundle to restore. onRestoreInstanceState() is only called when there is a Bundle to restore."
我将应用程序被终止后恢复所需的所有 session 数据移动到可序列化的单例中。
在 onSaveInstanceState() 中,我将序列化的 session 数据放入 savedInstanceState 包中
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
// save and restore session data to instance state to recover from task termination
savedInstanceState.putSerializable(SessionVariables.class.getName(), mSessionVariables);
}
在 onCreate() 和 onRestoreInstanceState() 中,我测试我的 session 单例实例是否有效。如果它没有有效数据,我会从 savedInstanceState 包中恢复 session 变量,并将这个恢复的对象作为我的 session 变量单例。
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
if (mSessionVariables.propertyThatShouldbeGood == null || mSessionVariables.propertyThatShouldbeGood .length() == 0)
{
// save and restore session data to instance state to recover from task termination
Serializable serializedSessionVariables = savedInstanceState.getSerializable(SessionVariables.class.getName());
if (serializedSessionVariables != null) {
mSessionVariables = (SessionVariables) serializedSessionVariables;
SessionVariables.putInstance(mSessionVariables);
}
}
}
现在,当我的任务被终止时,Android 会从堆栈中恢复之前的 Activity 。 onCreate() 从保存的实例状态包中恢复所有必要的 session 数据。
此时后退按钮也可以正常工作,所以我错了 Android 没有保留 Activity 堆栈。我认为它只是根据需要恢复后退 Activity (当您向后导航时)。如果您不返回,则不会创建它们。至少在 HierarchyViewer 中是这样的。
当我的堆栈是 A-B-C-D 并且任务被终止时, Activity “C”恢复并且“D”丢失。但是,现在“C”处于健康状态,用户可以轻松导航回“D”。现在,我还可以从“C”自动启动“D” Activity ,只是我还没有时间。
关于android - 进程停止后控制 Android Activity 重启,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22429197/
我有一个奇怪的问题:我在rvm上安装了rubyonrails。一切正常,我可以创建项目。但是在我输入“railsnew”时重新启动后,我有“程序'rails'当前未安装。”。SystemUbuntu12.04ruby-v"1.9.3p194"gemlistactionmailer(3.2.5)actionpack(3.2.5)activemodel(3.2.5)activerecord(3.2.5)activeresource(3.2.5)activesupport(3.2.5)arel(3.0.2)builder(3.0.0)bundler(1.1.4)coffee-rails(
我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server
在MRIRuby中我可以这样做:deftransferinternal_server=self.init_serverpid=forkdointernal_server.runend#Maketheserverprocessrunindependently.Process.detach(pid)internal_client=self.init_client#Dootherstuffwithconnectingtointernal_server...internal_client.post('somedata')ensure#KillserverProcess.kill('KILL',
我正在编写一个gem,我必须在其中fork两个启动两个webrick服务器的进程。我想通过基类的类方法启动这个服务器,因为应该只有这两个服务器在运行,而不是多个。在运行时,我想调用这两个服务器上的一些方法来更改变量。我的问题是,我无法通过基类的类方法访问fork的实例变量。此外,我不能在我的基类中使用线程,因为在幕后我正在使用另一个不是线程安全的库。所以我必须将每个服务器派生到它自己的进程。我用类变量试过了,比如@@server。但是当我试图通过基类访问这个变量时,它是nil。我读到在Ruby中不可能在分支之间共享类变量,对吗?那么,还有其他解决办法吗?我考虑过使用单例,但我不确定这是
当我在Rails控制台中按向上或向左箭头时,出现此错误:irb(main):001:0>/Users/me/.rvm/gems/ruby-2.0.0-p247/gems/rb-readline-0.4.2/lib/rbreadline.rb:4269:in`blockin_rl_dispatch_subseq':invalidbytesequenceinUTF-8(ArgumentError)我使用rvm来管理我的ruby安装。我正在使用=>ruby-2.0.0-p247[x86_64]我使用bundle来管理我的gem,并且我有rb-readline(0.4.2)(人们推荐的最少
我正在使用Ruby2.1.1和Rails4.1.0.rc1。当执行railsc时,它被锁定了。使用Ctrl-C停止,我得到以下错误日志:~/.rvm/gems/ruby-2.1.1/gems/spring-1.1.2/lib/spring/client/run.rb:47:in`gets':Interruptfrom~/.rvm/gems/ruby-2.1.1/gems/spring-1.1.2/lib/spring/client/run.rb:47:in`verify_server_version'from~/.rvm/gems/ruby-2.1.1/gems/spring-1.1.
我将我的Rails应用程序部署到OpenShift,它运行良好,但我无法在生产服务器上运行“Rails控制台”。它给了我这个错误。我该如何解决这个问题?我尝试更新rubygems,但它也给出了权限被拒绝的错误,我也无法做到。railsc错误:Warning:You'reusingRubygems1.8.24withSpring.UpgradetoatleastRubygems2.1.0andrun`gempristine--all`forbetterstartupperformance./opt/rh/ruby193/root/usr/share/rubygems/rubygems
说在前面这部分我本来是合为一篇来写的,因为目的是一样的,都是通过独立按键来控制LED闪灭本质上是起到开关的作用,即调用函数和中断函数。但是写一篇太累了,我还是决定分为两篇写,这篇是调用函数篇。在本篇中你主要看到这些东西!!!1.调用函数的方法(主要讲语法和格式)2.独立按键如何控制LED亮灭3.程序中的一些细节(软件消抖等)1.调用函数的方法思路还是比较清晰地,就是通过按下按键来控制LED闪灭,即每按下一次,LED取反一次。重要的是,把按键与LED联系在一起。我打算用K1来作为开关,看了一下开发板原理图,K1连接的是单片机的P31口,当按下K1时,P31是与GND相连的,也就是说,当我按下去时
最近因为项目需要,需要将Android手机系统自带的某个系统软件反编译并更改里面某个资源,并重新打包,签名生成新的自定义的apk,下面我来介绍一下我的实现过程。APK修改,分为以下几步:反编译解包,修改,重打包,修改签名等步骤。安卓apk修改准备工作1.系统配置好JavaJDK环境变量2.需要root权限的手机(针对系统自带apk,其他软件免root)3.Auto-Sign签名工具4.apktool工具安卓apk修改开始反编译本文拿Android系统里面的Settings.apk做demo,具体如何将apk获取出来在此就不过多介绍了,直接进入主题:按键win+R输入cmd,打开命令窗口,并将路
在我的Character模型中,我添加了:字符.rbbefore_savedoself.profile_picture_url=asset_path('icon.png')end但是,对于数据库中已存在的所有角色,它们的profile_picture_url为nil。因此,我想进入控制台并遍历所有这些并进行设置。在我试过的控制台中:Character.find_eachdo|c|c.profile_picture_url=asset_path('icon.png')end但这给出了错误:NoMethodError:undefinedmethod`asset_path'formain:O