Activity not destroying to release Heap in android?

931 views Asked by At

I am trying to release heap size by destroying the current activity, while going to another activity.

I am using finish(); on backPreess()

But this is not releasing the heap.

on setContentView()

The heap size increases 16Mb. I want to release this increase in the heap after going to another activity. Can any one help how to do this?

My code is as following:

    package com.stancil.levels;        
    public class PaintActivity extends ZebraActivity implements
            PaintView.LifecycleListener, PaintView1.LifecycleListener1 {

        private static final int REQUEST_PICK_COLOR = 1;

        ....
....
    public PaintActivity() {
            _state = new State();
        }

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            Constants.context = getApplicationContext();


            setContentView(R.layout.paint);
            ..................
    ...................
    ...............

        }


        public void onPreparedToLoad() {
            // We need to invoke InitPaintView in a callback otherwise
            // the visibility changes do not seem to be effective.
            new Handler() {
                @Override
                public void handleMessage(Message m) {
                        new InitPaintView();

                    Log.v("PaintActivity", "After InitPaintView Called");
                }
            }.sendEmptyMessage(0);
        }


        private class InitPaintView implements Runnable {
            private Bitmap _originalOutlineBitmap;
            private Handler _handler;

            public InitPaintView() {
                // Make the progress bar visible and hide the view

                _paintView.setVisibility(View.GONE);
                _progressBar.setProgress(0);
                _progressBar.setVisibility(View.VISIBLE);
                _state._savedImageUri = null;
                _state._loadInProgress = true;

                _originalOutlineBitmap=_imageBitmap;
                _handler = new Handler() {
                    @Override
                    public void handleMessage(Message m) {
                        switch (m.what) {
                        case Progress.MESSAGE_INCREMENT_PROGRESS:
                            // Update progress bar.
                            _progressBar.incrementProgressBy(m.arg1);
                            break;
                        case Progress.MESSAGE_DONE_OK:
                        case Progress.MESSAGE_DONE_ERROR:
                            // We are done, hide the progress bar 
                            // the paint view back on.
                            _state._loadInProgress = false;
                            _paintView.setVisibility(View.VISIBLE);
                            _progressBar.setVisibility(View.GONE);
                            initiatePopupWindow();
                            break;
                        }
                    }
                };

                new Thread(this).start();

            }

            public void run() {
                Log.v("Wasimmmmmmmmmmmmmmmm", "qqqqq 22");
                _paintView.loadFromBitmap(_originalOutlineBitmap, _handler);
            }

        }



        private static class State {
            // Are we just loading a new outline?
            public boolean _loadInProgress;

            // The resource ID of the outline we are coloring.
            //public int _loadedResourceId;
            //
            // If we have already saved a copy of the image, we store the URI here
            // so that we can delete the previous version when saved again.
            public Uri _savedImageUri;
        }







        @Override
            public void onBackPressed() {
                new AlertDialog.Builder(this)
                .setTitle("Exit")
                .setMessage("Do you want to go to Main Menu?")
                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {

                         Constants.check_new=true;
                       Intent i=new Intent(PaintActivity.this,MainActivity.class);
                      // i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                       startActivity(i);
                       finish();
                       overridePendingTransition(R.anim.fade_in, R.anim.fade_out);

                    }
                }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        // Do nothing.
                    }
                }).show();
         }
}

    }
1

There are 1 answers

2
Maciej Boguta On

release yoru objects in onDestroy method, anyways, if there are no references to the detroyed activity, GC will automaticly clean up whenever its needed (it doesnt need to happen right after you closed your activity). Alternatively theres a method to force running GC, but I wont even write about it cuz its not really a feature a typical application should use