Android的 - 为什么用onSaveInsanceState()来保存位图对象不会被调用? [英] Android - Why is using onSaveInsanceState() to save a bitmap object not being called?

查看:348
本文介绍了Android的 - 为什么用onSaveInsanceState()来保存位图对象不会被调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做一个简单的绘图应用程序。

I'm making a simple drawing application.

我希望能够保存屏幕当设备方向变化对用户的绘图。这仅发生在主要活动。

I want to be able to save the user's drawing on the screen when the device orientation changes. This only happens in the main activity.

我看了,如果方向变化,那么该活动破坏并重新创建再次(的onCreate(捆绑创建)被调用)。
我不知道这是否意味着它也应该调用 onSavedInstanceState(束束),因为在我的应用程序,如果另一项活动把焦点之上它只是叫我主要活动,但不旋转到横向/纵向的时候。

I read that if the orientation changes, then the activity is destroyed and recreated again (onCreate(Bundle created) being called). I'm not sure if it means that it should also call onSavedInstanceState(Bundle bundle), because in my app it is only called if another activity takes the focus on top of my main activity, but not when rotating to landscape/portrait.

我只是在寻找一种方式来保存现有的位图,并将其传递到主活动时的方向变化。我该怎么办呢,如果我的的onSaveInstanceState 不会被调用?

I'm simply looking for a way to save an existing bitmap and pass it to the main activity when the orientation changes. How can I do it if my onSaveInstanceState never gets called?

此外,由于位图工具 parceable 我已经用它。

Also, since Bitmap implements parceable already I used it.

下面是从主活动code:

Here's the code from the main activity:

 public void onCreate(Bundle savedInstanceState) {      
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
// some more activity code...

        if (savedInstanceState != null) {
        bitmap = savedInstanceState.getParcelable("bitmap");
        Log.d("STATE-RESTORE", "bitmap created");
        paintBoard.setBitmapBackground(bitmap, false);
        Log.d("RESTORING...", "onRestoreInstanceState()");
    } else {
        Log.d("SavedInstanceState", "null");
    }
}


// Never called when I change orientation on my device
@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    bitmap = Bitmap.createBitmap(paintBoard.getBitmap());
    outState.putParcelable("bitmap", bitmap);
    Log.d("STATE-SAVE", "onSaveInstanceState()");
}

任何帮助将AP preciated。

Any help will be appreciated.

编辑:

我删除了AndroidManifest.xml文件这一行:

I removed this line from the AndroidManifest.xml file:

android:configChanges="orientation"

现在的onSaveInstanceState()当我更改设备上的取向不会被调用。

and now onSaveInstanceState() does get called when I change orientation on the device.

推荐答案

保存在的onPause位图()的onDestroy(),因为我觉得 onSavedInstanceState()永远不会被Android系统正所谓切换时的方向。

Save your bitmap in onPause() or onDestroy() because i think onSavedInstanceState() is never called by the android sytem when switching orientation.

这篇关于Android的 - 为什么用onSaveInsanceState()来保存位图对象不会被调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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