Android正在杀死我的应用程序,只要startactivityforresult被称为 [英] Android is killing my application whenever startactivityforresult is called

查看:247
本文介绍了Android正在杀死我的应用程序,只要startactivityforresult被称为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗯,在我的应用程序正在使用默认的摄像头和Android的画廊startActivityforResult为;

 意向书我=新的意向书(android.intent.action.PICK
                       MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
i.setType(图像/ *);
startActivityForResult(ⅰ,this.SELECT_PICTURE);
 

我能赶上onActivityResult为好。但是,如果内存不足,我开始这个活动,我经历了系统是杀害我父母的活动,即使该活动返回一个值,我失去了我的所有局部变量(和的onCreate()再次调用)。只有onResume()应该被再次调用。

我试了好几次,我是pretty的肯定,它与内存问题有关。如果成功完成足够的存储过程。我怎样才能prevent这种行为?

由于Android不杀的应用前景,可我仍然可以使用这个功能吗?我想我的问题是与<一个几乎相同href="http://stackoverflow.com/questions/3754360/android-startactivityforresult-is-killing-a-thread-within-the-parent-activity">this 之一。


解决方案

在除溶液 Yahel 提供了以下方法使用可以正常工作。

  @覆盖
公共无效的onSaveInstanceState(包savedInstanceState){
   super.onSaveInstanceState(savedInstanceState);
}

@覆盖
公共无效onRestoreInstanceState(包savedInstanceState){
   super.onRestoreInstanceState(savedInstanceState);
}
 

解决方案

您需要一个良好的阅读必读 - 前 - 你code文章:<一href="http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle">http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle

您活动中的能够而且将会搭载Android杀死,如果内存不足。但它会得到通知,并有一个非常明确的方式处理这个问题。

这是生存的捆绑savedInstanceState 在所有活动中的原因OnCreate中:公共无效的onCreate(包savedInstanceState)

在你的应用程序的的onPause,应保存所有你需要重新创建状态的程序是和你的应用程序的OnCreate你应该检查,如果你回来,尽显savedInstanceState,并从那里恢复您的应用程序中。

祝你好运。

Well, in my application I'm using default camera and gallery of Android with startActivityforResult as;

Intent i = new Intent("android.intent.action.PICK",
                       MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
i.setType("image/*");
startActivityForResult(i, this.SELECT_PICTURE);

I am able to catch onActivityResult as well. However, if memory is low and I start this activity, I experienced that system is killing my parent activity, and even if the activity returns with a value, I lost all my local variables (and onCreate() is called again). Only onResume() should be called again.

I tried several times, I'm pretty sure that it is related with memory issue. If there is enough memory process is performed successfully. How can I prevent this behaviour?

Since Android doesn't kill application in foreground, can I still use this feature? I think my problem is almost similar with this one .


Solution

In addition to solution provided by Yahel, usage of following methods can work.

@Override
public void onSaveInstanceState(Bundle savedInstanceState){
   super.onSaveInstanceState(savedInstanceState);
}

@Override
public void onRestoreInstanceState(Bundle savedInstanceState){
   super.onRestoreInstanceState(savedInstanceState);
}

解决方案

You need a good read of the must-read-before-you code article : http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle

You activitie can and will be killed by Android if memory is low. But it will be notified and there is a very well defined way of dealing with this.

This is the reason of existence of the Bundle savedInstanceState in all your activities oncreate : public void onCreate(Bundle savedInstanceState)

During the onpause of your app, you should save all you need to recreate the state your app is in and during the oncreate of your app you should check if you are coming back with a full savedInstanceState and restore your app from there.

Good luck.

这篇关于Android正在杀死我的应用程序,只要startactivityforresult被称为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆