简体   繁体   English

停止另一个活动时销毁活动

[英]Destroy Activity when another Activity is Stopped

I have a activity A (a list view) that calls Activity B (a adapter which retrieves allot of images from a database). 我有一个活动A(列表视图),它调用Activity B(一个从数据库中检索所有图像的适配器)。 When the user clicks the Back Button of Activity A the app returns to the main menu. 当用户单击活动A的后退按钮时,应用程序将返回主菜单。

The problem is I can still see Activity B running and getting all of data from the DB using up valuable memory. 问题是我仍然可以看到活动B正在运行并从数据库中获取所有数据占用宝贵的内存。

Is there a way when Activity A back button is pressed I can destroy Activity B? 有没有办法按下活动A后退按钮我可以销毁活动B?

Thanks Ciaran 谢谢Ciaran

Activity A is a list activity that opens DB, gets cursor object, sends to Itemadpter class to populate list view: 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();

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 编辑:CursorAdapter类,这里从DB中检索图像,调整大小,并设置为列表视图的ImageView ......即使ListAcivity已经完成占用大量内存,此过程仍在继续

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..... 编辑:此代码有效:取消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");

Ok. 好。 You are doing all stuff in activity B through an adapter class. 您正在通过适配器类在活动B中执行所有操作。 So if you want to stop B in your back key then just finish it in your onBackKeyPressed method. 所以如果你想在后面的键中停止B,那么只需在你的onBackKeyPressed方法中完成它。 For this you may try some suggestions (As i don't know your current flow). 为此您可以尝试一些建议(因为我不知道您当前的流程)。

1: call finish() in overriding onBackKeyPressed() . 1:在覆盖onBackKeyPressed()调用finish() onBackKeyPressed()

2: In your adapter class after finish all loading stuff you also can call finish through your current context. 2:在完成所有加载的东西后,在你的适配器类中,你也可以通过当前的上下文调用finish。 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. 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