活动没有破坏在Android中释放堆?

时间:2013-11-29 12:00:40

标签: android android-activity heap ondestroy activity-finish

我试图通过销毁当前活动来释放堆大小,同时转到另一个活动。

我在finish();

上使用backPreess()

但这不是释放堆。

on setContentView()

堆大小增加16Mb。我想在转到另一个活动后释放堆中的这种增加。任何人都可以帮忙怎么做?

我的代码如下:

    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 个答案:

答案 0 :(得分:1)

在onDestroy方法中释放yoru对象,无论如何,如果没有对被破坏活动的引用,GC会在需要时自动清理(它不需要在你关闭活动后立即发生)。 另外还有一种强制运行GC的方法,但我甚至都不会写它,因为它不是典型应用程序应该使用的功能