Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/225.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
动态更改src后保存Android映像状态_Android_Imageview_Onrestoreinstancestate - Fatal编程技术网

动态更改src后保存Android映像状态

动态更改src后保存Android映像状态,android,imageview,onrestoreinstancestate,Android,Imageview,Onrestoreinstancestate,我正在开发一个Android应用程序,我在一个页面上有一个imageView,只需长按一下,它就会从图像a变为图像B。但是,当他们离开页面时,imageView会返回到图像a。我如何保存状态(我猜它在暂停、停止和销毁时已经完成)以便它保存ImageView的当前图像src,并在下次访问和创建页面时将其加载。我从来没有用安卓系统保存过数据 任何简单的数据保存教程/示例都将不胜感激。以下内容将对您有所帮助: // Use a static tag so you're never debugging

我正在开发一个Android应用程序,我在一个页面上有一个imageView,只需长按一下,它就会从图像a变为图像B。但是,当他们离开页面时,imageView会返回到图像a。我如何保存状态(我猜它在暂停、停止和销毁时已经完成)以便它保存ImageView的当前图像src,并在下次访问和创建页面时将其加载。我从来没有用安卓系统保存过数据


任何简单的数据保存教程/示例都将不胜感激。

以下内容将对您有所帮助:

// Use a static tag so you're never debugging typos
private static final String IMAGE_RESOURCE = "image-resource";
private int image;
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    // if there's no bundle, this is the first time; use default resource
    if (savedInstanceState == null) {
        image = R.drawable.default;
    } else {
        // if there is a bundle, use the saved image resource (if one is there)
        image = savedInstanceState.getInt(IMAGE_RESOURCE, R.drawable.default);
    }
}

@Override
public void onSaveInstanceState(Bundle outState) {
    // Make sure you save the current image resource 
    outState.putInt(IMAGE_RESOURCE, image);
    super.onSaveInstanceState(outState);
}
确保在单击侦听器中更改图像变量的同时,将其设置为适当的资源


如果你想记住比这更长的状态,请仔细研究。

以下几点可以帮助你:

// Use a static tag so you're never debugging typos
private static final String IMAGE_RESOURCE = "image-resource";
private int image;
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    // if there's no bundle, this is the first time; use default resource
    if (savedInstanceState == null) {
        image = R.drawable.default;
    } else {
        // if there is a bundle, use the saved image resource (if one is there)
        image = savedInstanceState.getInt(IMAGE_RESOURCE, R.drawable.default);
    }
}

@Override
public void onSaveInstanceState(Bundle outState) {
    // Make sure you save the current image resource 
    outState.putInt(IMAGE_RESOURCE, image);
    super.onSaveInstanceState(outState);
}
确保在单击侦听器中更改图像变量的同时,将其设置为适当的资源


如果您希望记住的状态比这更长,请查看。

Hi Krylez,如果应用程序完全关闭,或者他们按了退出活动的后退按钮。这还会检索数据吗?我的意思是,当然,如果我把呼叫放在onDestroy/onStop等上,或者savedinstancestate在应用程序关闭时丢失了数据,那么当你关闭应用程序时,捆绑包就丢失了。它只存在于内存中,所以当Android操作系统关闭你的应用程序时,它就永远消失了。即使你的应用程序关闭,SharedReferences仍将保留。谢谢。我想我需要使用SharedReferences。嗨,Krylez,如果应用程序完全关闭,或者他们点击后退按钮退出活动。这还会检索数据吗?我的意思是,当然,如果我把呼叫放在onDestroy/onStop等上,或者savedinstancestate在应用程序关闭时丢失了数据,那么当你关闭应用程序时,捆绑包就丢失了。它只存在于内存中,所以当Android操作系统关闭你的应用程序时,它就永远消失了。即使你的应用程序关闭,SharedReferences仍将保留。谢谢。我想我需要使用SharedReference。