Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/321.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
Java 使用SharedReferences更改所有活动的背景_Java_Android_Onresume_Setbackground - Fatal编程技术网

Java 使用SharedReferences更改所有活动的背景

Java 使用SharedReferences更改所有活动的背景,java,android,onresume,setbackground,Java,Android,Onresume,Setbackground,我有一个活动,在其中我将可绘制内容存储在SharedReferences中,然后将其加载到其他活动的onResume()方法中以更改背景 像这样: @Override protected void onResume() { super.onResume(); int mSavedBackground = storageUtil.loadBackgroundId(); mMainLayout.setBackgroundResource(mSavedBackgroun

我有一个活动,在其中我将可绘制内容存储在SharedReferences中,然后将其加载到其他活动的onResume()方法中以更改背景

像这样:

@Override
 protected void onResume() {
    super.onResume(); 

    int mSavedBackground = storageUtil.loadBackgroundId();

    mMainLayout.setBackgroundResource(mSavedBackground);
}

但是总是在onResume()中调用
.setBackgroundResource
是否不好?

您的实际需求是什么?您可以在onCreate方法中调用它。通常,它不会产生太大的影响。因为资源将被缓存。如果对您有效,您还可以尝试在
onCreate()
中添加它。这取决于您的背景id资源分辨率。如果您的背景图像分辨率非常高,并且您非常频繁地调用setBackgroundResource,则会出错。@Froyo My parentActivity不会被重新创建,因为我使用的是launchmode singletop。@Nik My parentActivity不会被重新创建,因为我使用的是launchmode singletop