Ahmed Abd-Elmeged Ahmed Abd-Elmeged - 1 month ago 6
Android Question

Optimize splash screen memory

I made a splash screen and then started the activity as usual.

but i noticed that memory usage increased a lot even though i finished the activity and a big difference in memory usage between the splash with a background and the one with no background.

Any solution for this?

a capture of the memory when splash has no background image

enter image description here

a capture of the memory when splash has a background image

enter image description here

the code for the splash activity

public class SplashActivity extends AppCompatActivity {

/**
* To get ride of the activity reference to avoid memory leaks
*/
private static WeakReference<SplashActivity> mActivity;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);

mActivity = new WeakReference<>(this);

//delay for 2 seconds and start the home activity
Completable.complete()
.delay(2, TimeUnit.SECONDS)
.doOnComplete(this::startHomeActivity)
.subscribe();
}

private void startHomeActivity() {
if (mActivity.get() != null) {
Activity activity = mActivity.get();
Intent homeIntent = new Intent(activity, HomeActivity.class);
startActivity(homeIntent);
activity.finish();
}
}
}


and set the theme at manifest

<style name="splashScreenTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowBackground">@drawable/zamen_splash</item>
<item name="colorPrimaryDark">@color/splash_color_dark</item>
</style>

Answer Source

You should put your drawable inside the specific folder for the resolution your image is being using for.

You said that the image is 1920*1080. So it seem the resolution is for xxx-hdpi. If you put that image inside that specific folder you are telling the SO what the resolution of the phone should be so that it can use that image. In this case xxx-hdpi is for 4x dpi. This link will give you more information.

Then Android will adjust that to others resolutions and save you from memory waste. You can add an specific image for those resolutions inside the other folders (hdpi, xhdpi, xxhdpi) and avoid the SO to do the extra work.

Inside this link you will have information for different devices.

As many users said: there is no need of using weakrefrence.