0

I have a activity A (a list view) that calls Activity B (a adapter which retrieves allot of images from a database). When the user clicks the Back Button of Activity A the app returns to the main menu.

The problem is I can still see Activity B running and getting all of data from the DB using up valuable memory.

Is there a way when Activity A back button is pressed I can destroy Activity B?

Thanks Ciaran

Activity A is a list activity that opens DB, gets cursor object, sends to Itemadpter class to populate list view:

// get the cursor from database 

                        ViewListOfDives.data = new diveDataBase(ViewListOfDives.this);
                        ViewListOfDives.data.open();
                        // get cursor object holding all data, use a asynch inner class to load 
                        cursor = data.getCursorData();
//check if data available
                        if(cursor!=null && cursor.getCount()>0){
                        // get customised array adoater list
                        adapter = new ItemAdapter(ViewListOfDives.this, cursor);
                        }else{

                                //display o dives in data base message and finish this activity
                                displayDialog();

                        }
                        ViewListOfDives.this.setListAdapter(adapter);
                        ViewListOfDives.data.close();

EDIT: CursorAdapter class, here the images are retrieved from DB, resized, and set to ImageView of a list view.....this process continues even though the ListAcivity has finished using up lots of memory

This is all carried out in a asynch inner class...

public ItemAdapter(Context context, Cursor c) {
            super(context, c);
            mContext = context;
            mLayoutInflater = LayoutInflater.from(context); 

          // mContext.
           // noOfRows = c.getCount()+1;//use row count to get no of dives
        }//end constructor

//do in background method
//retrival of images from DB and resizing is carried out in a asynch class
String diveImagePath = imagePath[0];

                     File imagePathFile = new File(diveImagePath); 
                     try {
                        final int IMAGE_MAX_SIZE = 3000;
                            FileInputStream streamIn = new FileInputStream(imagePathFile);

                        // Decode image size and setInJBounds = true to avoid auto memory allocation for large image
                            BitmapFactory.Options o = new BitmapFactory.Options();
                            o.inJustDecodeBounds = true;
                           BitmapFactory.decodeStream(streamIn, null, o);
                             streamIn.close();

                            int scale = 1;
                            while ((o.outWidth * o.outHeight) * (1 / Math.pow(scale, 2)) > 
                           IMAGE_MAX_SIZE) {
                               scale++;
                            }

                           //get orginal width of image before loaded into memory 
                           Log.d(TAG, "scale = " + scale + ", orig-width: " + o.outWidth + "  orig-height: " + o.outHeight);


                           Bitmap b = null;
                           streamIn = new FileInputStream(imagePathFile);
                            if (scale > 1) {
                                scale--;
                                // scale to max possible inSampleSize that still yields an image
                                // larger than target, inSampleSize loads the image into memor by a factor of its integer value
                                o = new BitmapFactory.Options();
                                o.inSampleSize = scale;
                            // Decode bitmap with inSampleSize set
                               o.inJustDecodeBounds = false;

                               b = BitmapFactory.decodeStream(streamIn, null, o);
                              resizedImage = reSizeImage(b);
                             streamIn.close();
                             b.recycle();
                             System.gc();

                    } else {
                        bitmap = BitmapFactory.decodeStream(streamIn);
                       resizedImage = reSizeImage(bitmap);
                      streamIn.close();
                       System.gc();
                    }

@Override
                protected void onPostExecute(Bitmap bitmap) {

                    ImageView displayImage = (ImageView) view.findViewById(R.id.iv_list_image);
                    if(bitmap!=null){

                        displayImage.setBackground(null);
                        //resizedImage = reSizeImage(bitmap);

                        displayImage.setImageBitmap(resizedImage);


                    }else{
                        //Toast.makeText(context, "No Image Found!! Usimng default", Toast.LENGTH_LONG).show();
                        displayImage.setBackgroundResource(R.drawable.logdive3);
                    }

Edit: This code worked: Cancel both the ListActivity asynch task (which in turns calls the CursorAdpter ayscnh task for loading images from the DataBase), and get the CursorAdtpter aysnch task reference and cancel this also.....

//in the ListActivity class
@Override
    public void onBackPressed() {
        // try to quit cursoradpter from reriving and upload data when user clicks back button
        super.onBackPressed();
        //cancel the background process of asycn task
        getCursorAysnch.cancel(true);

        //now cancel backgound process of Itemadatpetr class to free memory and stop loading images from DB
        adapter.getImageAsynch.cancel(true);
        Log.d("Vuiew List Dives:", "Back button pressed");
dancingbush
  • 2,131
  • 5
  • 30
  • 66

2 Answers2

2

Ok. You are doing all stuff in activity B through an adapter class. So if you want to stop B in your back key then just finish it in your onBackKeyPressed method. For this you may try some suggestions (As i don't know your current flow).

1: call finish() in overriding onBackKeyPressed().

2: In your adapter class after finish all loading stuff you also can call finish through your current context. But here you have to do all things through casting to an activity as the adapter is not an activity and we know only the activity can be finish.

Ya the cast should as Rat-a-tat-a-tat Ratatouille said, but do it after you finish all getting stuff from database otherwise it will stop all your running things from activity B.

Ranjit
  • 5,130
  • 3
  • 30
  • 66
  • Ok point 1 no problem, adpter class still running in background. Don't get point 2, how to quit the Adpter class retrieving data in the background...ill post code above – dancingbush Feb 23 '14 at 10:11
  • can you show your adapter class here .. how you know that it still running ? – Ranjit Feb 23 '14 at 10:15
  • yep ill post the log cat, its retrieving images from DB and resizing them continuously after the ListActivity is finished . – dancingbush Feb 23 '14 at 10:17
  • I think the problem is in your close method ..you have to close it after adapter = new ItemAdapter(ViewListOfDives.this, cursor); line from your if clause..try to close the db perfectly. – Ranjit Feb 23 '14 at 10:19
  • hmmm i close the DB in LstActivity in the onPostExecute after the ListAdpter is set to custom adapter, and also in the onBackPressed() method....its opened in the do-in-backgound method where adapter class is instantiated – dancingbush Feb 23 '14 at 10:26
0

Code for Cancel both the ListActivity asynch task (which in turns calls the CursorAdpter ayscnh task for loading images from the DataBase), and get the CursorAdtpter aysnch task reference and cancel this also.....please see edit to question for code, thanks all

dancingbush
  • 2,131
  • 5
  • 30
  • 66