unable to destroy activity Android

here is my java code

    @Override
    protected void onDestroy() {
    int i;
    super.onDestroy();
    if (bitmapList != null) {
        for (i = INDEX_COLLAGE; i < bitmapList.length; i += INDEX_COLLAGE_BACKGROUND) {
            if (bitmapList[i] != null) {
                bitmapList[i].recycle();
            }
        }
    }
    if (collageView != null) {
        if (collageView.shapeLayoutList != null) {
            for (i = INDEX_COLLAGE; i < collageView.shapeLayoutList.size(); i += INDEX_COLLAGE_BACKGROUND) {
                for (int j = INDEX_COLLAGE; j < collageView.shapeLayoutList.get(i).shapeArr.length; j += INDEX_COLLAGE_BACKGROUND) {
                    if (collageView.shapeLayoutList.get(i).shapeArr[j] != null) {
                        collageView.shapeLayoutList.get(i).shapeArr[j].freeBitmaps();
                    }
                }
            }
        }
        if (collageView.maskBitmapArray != null) {
            for (i = INDEX_COLLAGE; i < collageView.maskBitmapArray.length; i += INDEX_COLLAGE_BACKGROUND) {
                if (collageView.maskBitmapArray[i] != null) {
                    if (!collageView.maskBitmapArray[i].isRecycled()) {
                        collageView.maskBitmapArray[i].recycle();
                    }
                    collageView.maskBitmapArray[i] = null;
                }
            }
        }
    }
    if (adWhirlLayout != null) {
        adWhirlLayout.removeAllViews();
        adWhirlLayout.destroy();
    }
}
private void backButtonAlertBuilder() {
    AlertDialog.Builder builder = new AlertDialog.Builder(CreateCollageActivity.this);
    builder.setMessage("Would you like to save image ?").setCancelable(true).setPositiveButton("Yes", new OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            if (analytics != null)
                analytics.logEvent(Analytics.Param.IMAGE_SAVE, "");
            new SaveImageTask().execute();
        }
}).setNeutralButton("No", new OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {

            finish();
        }
    });

The app crashes when i click back button..when i debugged the code it showed java.lang.RuntimeException: Unable to destroy activity and HVDactivities.CreateCollageActivity.onDestroy(CreateCollageActivity.java:740)

2 answers

  • answered 2020-11-09 10:43 Moomen S. Aldahdouh

    Just replace onDestroy() method with onBackPressed()

    @Override
    public void onBackPressed() {
       super.onBackPressed();
    }
    

    So your code will be like that

    @Override
        protected void onBackPressed() {
        int i;
        super.onBackPressed();
        if (bitmapList != null) {
            for (i = INDEX_COLLAGE; i < bitmapList.length; i += INDEX_COLLAGE_BACKGROUND) {
                if (bitmapList[i] != null) {
                    bitmapList[i].recycle();
                }
            }
        }
        if (collageView != null) {
            if (collageView.shapeLayoutList != null) {
                for (i = INDEX_COLLAGE; i < collageView.shapeLayoutList.size(); i += INDEX_COLLAGE_BACKGROUND) {
                    for (int j = INDEX_COLLAGE; j < collageView.shapeLayoutList.get(i).shapeArr.length; j += INDEX_COLLAGE_BACKGROUND) {
                        if (collageView.shapeLayoutList.get(i).shapeArr[j] != null) {
                            collageView.shapeLayoutList.get(i).shapeArr[j].freeBitmaps();
                        }
                    }
                }
            }
            if (collageView.maskBitmapArray != null) {
                for (i = INDEX_COLLAGE; i < collageView.maskBitmapArray.length; i += INDEX_COLLAGE_BACKGROUND) {
                    if (collageView.maskBitmapArray[i] != null) {
                        if (!collageView.maskBitmapArray[i].isRecycled()) {
                            collageView.maskBitmapArray[i].recycle();
                        }
                        collageView.maskBitmapArray[i] = null;
                    }
                }
            }
        }
        if (adWhirlLayout != null) {
            adWhirlLayout.removeAllViews();
            adWhirlLayout.destroy();
        }
    }
    private void backButtonAlertBuilder() {
        AlertDialog.Builder builder = new AlertDialog.Builder(CreateCollageActivity.this);
        builder.setMessage("Would you like to save image ?").setCancelable(true).setPositiveButton("Yes", new OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                if (analytics != null)
                    analytics.logEvent(Analytics.Param.IMAGE_SAVE, "");
                new SaveImageTask().execute();
            }
    }).setNeutralButton("No", new OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
    
                finish();
            }
        });
    

  • answered 2020-11-09 11:17 Priyanka Rajput

    You should write all the code before super.onDestroy(); Make this line the last statement of the method.