I use the image to set as the background for all my activities, but this causes a memory overflow problem and causes the application to crash. Now I turn off my drawables on pause () and Destroy () in my activity, and now it shows a blank screen when I press the return button. So how can I avoid this without using extra memory.
protected void onPause(){ super.onPause(); unbindDrawables(findViewById(R.id.login_root)); } protected void onDestroy() { unbindDrawables(findViewById(R.id.login_root)); super.onDestroy(); } private void unbindDrawables(View view) { System.gc(); Runtime.getRuntime().gc(); if (view.getBackground() != null) { view.getBackground().setCallback(null); } if (view instanceof ViewGroup) { for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) { unbindDrawables(((ViewGroup) view).getChildAt(i)); } ((ViewGroup) view).removeAllViews(); }
Initially, I inflated my layout using the android: background = "@ drawable /", which always caused a memory overflow error, saying that the VM does not allow us to allocate 10 MB (application). Now I get the bitmap from this drawing without scaling down and bind it to runtime.Now he says that VM will not allow us to allocate 5 MB (application) without using unbindDrawables (..) Obviously, the quality of the background image that is shown has decreased , but I canβt understand that if I use a 13 KB png file, how does the JVM require 5 or 10 MB of space to process the request?
I transfer my build instructions from the onCreate () method to the onResume () method, but when I click the application again, it presses the back button.
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } protected void onResume(){ setContentView(R.layout.home); Bitmap bmp; ImageView background = (ImageView)findViewById(R.id.iv_home_background); InputStream is = getResources().openRawResource(R.drawable.background); bmp = BitmapFactory.decodeStream(is); background.setImageBitmap(bmp); super.onResume(); } protected void onPause(){ super.onPause(); unbindDrawables(findViewById(R.id.home_root)); } private void unbindDrawables(View view) { System.gc(); Runtime.getRuntime().gc(); if (view.getBackground() != null) { view.getBackground().setCallback(null); } if (view instanceof ViewGroup) { for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) { unbindDrawables(((ViewGroup) view).getChildAt(i)); } ((ViewGroup) view).removeAllViews(); } }
source share