2014-02-23 66 views
0

我有一个活动A(一个列表视图),它调用活动B(一个从数据库检索图像分配的适配器)。当用户点击活动A的返回按钮时,该应用程序返回到主菜单。当另一个活动停止时销毁活动

问题是我仍然可以看到活动B正在运行并从数据库中获取所有数据,使用了有价值的内存。

活动A后退按钮被按下时是否有办法我可以销毁活动B?

感谢 夏兰

活性A被打开数据库,获取光标对象名单的活动,发送到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类,这里的图像从数据库检索,调整大小,并设置为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任务从数据库中装载图像),并获得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"); 
+2

B是一个活动或一个适配器类? – Ranjit

+0

B是一个cursoradapter类...... – dancingbush

+0

为什么不通过你的上下文从你的适配器完成活动.. – Ranjit

回答

2

好的。您正在通过适配器类来完成活动B中的所有内容。所以如果你想在后退键中停止B,那么只需在你的onBackKeyPressed方法中完成。为此,你可以尝试一些建议(因为我不知道你目前的流量)。

1:致电finish()覆盖onBackKeyPressed()

2:在您的适配器类完成所有加载的东西之后,您还可以通过当前上下文调用finish。但是在这里,你必须通过投射到活动来做所有事情,因为适配器不是一项活动,我们知道只有活动才能完成。

雅演员应该梆,一达料理鼠王说,但做到这一点你完成从数据库中的所有东西得到后否则将停止活动B.

+0

好点1没问题,adpter班级仍在后台运行。不要得到第2点,如何退出Adpter类在后台检索数据...病后邮政编码 – dancingbush

+0

上面可以显示您的适配器类在这里..你怎么知道它仍然在运行? – Ranjit

+0

是的生病日志猫,它从DB检索图像,并在ListActivity完成后不断调整它们的大小。 – dancingbush

0

代码为所有正在运行的东西取消两个ListActivity异步任务(依次调用CursorAdpter ayscnh任务以从DataBase中加载图像),并获取CursorAdtpter aysnch任务参考并取消此操作.....请参阅编辑以查询代码,谢谢所有

相关问题