活动的生命周期 - 的onCreate呼吁每一个重新定位 [英] Activity lifecycle - onCreate called on every re-orientation
问题描述
我有一个加载在的onCreate
的位图一个简单的活动。我觉得,如果我旋转设备,我可以从的onCreate
再次调用日志看。事实上,由于所有的实例变量设置为再次默认值,我知道,整个活动已被重新实例化。
I have a simple activity that loads a bitmap in onCreate
. I find that if I rotate the device I can see from the logs that onCreate
called again. In fact, because all instance variables are set to default values again I know that the entire Activity has been re-instantiated.
旋转2次后,我得到了FC,因为没有足够的内存可以分配为位图。 (是activty的所有实例还活着的地方?抑或是GC不清理速度不够快?)
After rotating 2 times I get an FC because not enough memory can be allocated for the bitmap. (Are all instances of the activty still alive somewhere? Or does the GC not clean up fast enough?)
@Override
public void onCreate(Bundle savedInstanceState) {
File externalStorageDir = Environment.getExternalStorageDirectory();
File picturesDir = new File(externalStorageDir, "DCIM/Camera");
File[] files = picturesDir.listFiles(new FilenameFilter(){
public boolean accept(File dir, String name) {
return name.toLowerCase().endsWith(".jpg");
}});
if (files.length > 0) {
Bitmap bm = BitmapFactory.decodeStream(new FileInputStream(files[0]));
ImageView view = (ImageView) findViewById(R.id.photo);
view.setImageBitmap(bm);
}
}
这一切,我读,的onCreate应的应用程序的生命周期中被调用一次。难道我错了吗?如何可以重新定向设备事业活动要重现?
From all that I read, onCreate should be called once during the lifetime of an application. Am I wrong about this? How can re-orienting the device cause the activity to be recreated?
推荐答案
活动每次旋转后默认重建。您可以覆盖与 configChanges
的活动
标签中AndroidManifest属性此行为。有关详细信息和不同的选项,请参阅<一href="http://developer.android.com/guide/topics/resources/runtime-changes.html">http://developer.android.com/guide/topics/resources/runtime-changes.html
Activity is recreated after each rotation by default. You can override this behaviour with configChanges
attribute of the activity
tag in AndroidManifest. For further details and different options, see http://developer.android.com/guide/topics/resources/runtime-changes.html
这篇关于活动的生命周期 - 的onCreate呼吁每一个重新定位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!