簡體   English   中英

停止另一個活動時銷毀活動

[英]Destroy Activity when another Activity is Stopped

我有一個活動A(列表視圖),它調用Activity B(一個從數據庫中檢索所有圖像的適配器)。 當用戶單擊活動A的后退按鈕時,應用程序將返回主菜單。

問題是我仍然可以看到活動B正在運行並從數據庫中獲取所有數據占用寶貴的內存。

有沒有辦法按下活動A后退按鈕我可以銷毀活動B?

謝謝Ciaran

Activity A是一個列表活動,它打開DB,獲取游標對象,發送到Itemadpter類來填充列表視圖:

// 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();

編輯:CursorAdapter類,這里從DB中檢索圖像,調整大小,並設置為列表視圖的ImageView ......即使ListAcivity已經完成占用大量內存,此過程仍在繼續

這都是在異步內部階級進行的......

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);
                    }

編輯:此代碼有效:取消ListActivity異步任務(它依次調用CursorAdpter ayscnh任務從DataBase加載圖像),並獲取CursorAdtpter aysnch任務參考並取消此操作.....

//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");

好。 您正在通過適配器類在活動B中執行所有操作。 所以如果你想在后面的鍵中停止B,那么只需在你的onBackKeyPressed方法中完成它。 為此您可以嘗試一些建議(因為我不知道您當前的流程)。

1:在覆蓋onBackKeyPressed()調用finish() onBackKeyPressed()

2:在完成所有加載的東西后,在你的適配器類中,你也可以通過當前的上下文調用finish。 但是在這里你必須通過轉換到一個活動來做所有事情,因為適配器不是一個活動,我們只知道活動可以完成。

Ya演員應該像Ratatou-tat-a-tat Ratatouille所說的那樣,但是在完成從數據庫中獲取所有東西之后再這樣做,否則它將阻止所有來自活動B的所有東西。

取消ListActivity異步任務的代碼(它依次調用CursorAdpter ayscnh任務從DataBase加載圖像),並獲取CursorAdtpter aysnch任務參考並取消這個.....請看編輯問題代碼,謝謝所有

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM