2012-01-08 137 views
0

我在我的应用程序中有两个活动。“后退按钮”不关闭活动

第一个是地图,其中包含来自BBDD的项目叠加过程作为地图点。如果我点击其中的一个点,它会显示我的mostrarLugar活动,该活动只显示用户选择的网站的名称,说明和照片。

我有下一个问题: 如果在我的BBDD中有10个网站,当我看到其中一个网站时,如果我点击后退按钮,我必须做10个后退按钮点击,因为应用程序重新加载相同活动10次,全部使用相同的数据。

对于问题出在哪里,我没有任何想法。我试图强制完成一个

public boolean onKeyDown(int keyCode, KeyEvent event) 

但没有任何反应。

我认为这个问题可能在onTap方法中。这里是我的活动:

public class Capas extends ItemizedOverlay<OverlayItem> implements OnGestureListener 

    private GestureDetector gestureDetector; 
    Context contexto; 
    MapView Map; 
    private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>(); 
    private final Activity parent; 
    DataBaseHelper ayudabbdd; 
    MotionEvent evento; 



    public Capas(Drawable defaultMarker, Context context, Activity parent) 
     { 
      super(boundCenterBottom(defaultMarker)); 
      contexto = context; 
      gestureDetector = new GestureDetector((OnGestureListener)this); 
      this.parent = parent; 
      Map= (MapView) parent.findViewById(R.id.mapaTab); 
      contexto = parent.getApplicationContext(); 

     } 

    /**** 
    * Evento que se inicia al tocar 
    * para detectar el tipo de movimiento 
    * 
    *****/ 
    @SuppressWarnings("static-access") 
    @Override 
    public boolean onTouchEvent(MotionEvent event, MapView mapView) { 
       if(this.gestureDetector.onTouchEvent(event)) 
       { 
        return true; } 
     else{ 
      evento = event.obtain(event); 
      Map = mapView; 
      return super.onTouchEvent(event, mapView); 
     } 
    } 

    public boolean onTap(int index) { 


     return onTap(index, evento, Map); 
    } 

    public boolean onTap(int index,MotionEvent e, MapView map) { 

     ayudabbdd = new DataBaseHelper(contexto); 

     ArrayList<Object> datosLugar = ayudabbdd.getLugarPorID(index+1); 
     //Paso los datos de array a las variables 
     String nombre = (String) datosLugar.get(1).toString(); 
     muestraElPunto(nombre); 
     ayudabbdd.close(); 

     return super.onTap(index); 
    } 

而这是showMeSite活动。 我不认为这是错误的来源,但我也包括它。

public class mostrarLugar extends Activity{ 

    Context context; 
    DataBaseHelper ayudabbdd; 
    int id; 
    String nombre; 

    public void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.mostrarlugar); 

     View nombreDelLugar = (TextView)findViewById(R.id.tnombreLugar); 
     View descDelLugar = (TextView) findViewById(R.id.tDescLugar); 


     /******************** 
     * Obtengo los datos del item, con intent procedente 
     * de otro activity   
     ************************/ 

     String nombreClick = null; 
     final Bundle extras = getIntent().getExtras(); 



     if(extras!=null){ 
      nombreClick = extras.getString("nombre"); 
      } 

     //Conectamos a la base de datos y obtenemos el array de datos 
     ayudabbdd = new DataBaseHelper(this); 
     ArrayList<Object> datosLugar = ayudabbdd.getLugarPorNombre(nombreClick); 
     //Paso los datos de array a las variables 
     nombre = (String) datosLugar.get(1).toString(); 
     String descripcion = (String) datosLugar.get(2).toString(); 
     String foto = (String)datosLugar.get(3).toString(); 
     //Los seteo 
     ((TextView) nombreDelLugar).setText(nombre); 
     ((TextView) descDelLugar).setText(descripcion); 
     int compienzoBusquedaExtension = foto.length() - 4; 
     String sFoto;//String del path real de la foto 
     if(foto.indexOf(".jpg",compienzoBusquedaExtension)!=-1) 
      sFoto=foto; 
     else 
      sFoto=obtenerPatchReal(Uri.parse(foto)); 
     BitmapFactory.Options options=new BitmapFactory.Options(); 
     options.inSampleSize = 5; 
     Bitmap bitmap = BitmapFactory.decodeFile(sFoto, options); 
     ImageView iLugar = (ImageView) findViewById(R.id.iDelLugar); 
     iLugar.setImageBitmap(bitmap); 

    } 

     @Override 
     public void onDestroy() 
     { 
      ayudabbdd.close(); 
      super.onDestroy(); 
     } 
     /* 
     * Si presiona atras, cerramos la aplicacion 
     */ 
     @Override 
     public boolean onKeyDown(int keyCode, KeyEvent event) 
     { 
      if ((keyCode == KeyEvent.KEYCODE_BACK)) 
      { 
       finish(); 
      } 
      return super.onKeyDown(keyCode, event); 
     } 

这是getLugarPorID方法,我在和Ontap方法使用:

/********************************************* 
     * Obtener un lugar completo a partir del ID 
     * @param ID del lugar que queremos saber 
     * @return Un array con los datos del lugar 
     ***********************************************/ 
     public ArrayList<Object> getLugarPorID(int id) 
     { 
      CustomSQLiteOpenHelper helper = new CustomSQLiteOpenHelper(context); 
      db = helper.getWritableDatabase(); 

      ArrayList<Object> rowArray = new ArrayList<Object>(); 
      Cursor cursor; 

      try 
      { 

       cursor = db.query 
       (
         TABLE_NAME,new String[]{TABLE_ROW_ID, CNOMBRE, CDESC, CFOTO,CLAT,CLONG},TABLE_ROW_ID+"="+id,null, null, null, null, null 
       ); 

       //movemos el puntero a la primera posicion 
       cursor.moveToFirst(); 

       // Si hay datos los añado al array que sera devuelto 
       if (!cursor.isAfterLast()) 
       { 
        do 
        { 
         rowArray.add(cursor.getLong(0)); 
         rowArray.add(cursor.getString(1)); 
         rowArray.add(cursor.getString(2)); 
         rowArray.add(cursor.getString(3)); 
         rowArray.add(cursor.getDouble(4)); 
         rowArray.add(cursor.getDouble(5)); 

        } 
        while (cursor.moveToNext()); 
       } 

       cursor.close(); 
       db.close(); 
       return rowArray; 


      } 
      catch (SQLException e) 
      { 
       Log.e("Error obteniendo datos de lugar", e.toString()); 
       e.printStackTrace(); 
      } 
      return rowArray; 

     } 

回答

2

应该

@Override 
    public boolean onKeyDown(int keyCode, KeyEvent event) 
    { 
     if ((keyCode == KeyEvent.KEYCODE_BACK)) 
     { 
      finish(); 
     return true; // you missed this line 
     } 
     return super.onKeyDown(keyCode, event); 
    } 
+0

相同的结果,表明我同样的活动很多次 – colymore 2012-01-08 23:54:10

+0

你是什么意思很多次? – 2012-01-09 00:00:51

+0

我留在地图活动,这在我的地图中绘制4个intempizedoverlay点,例如,我点击其中的一个,打开mostrarLugar.activity,正确显示我的网站,按回来按钮,并再次显示我的网站,我必须按返回按钮4次以进入上一个活动。如果我在BBDD有10个网站,我必须按10次返回按钮才能返回.. 对不起,我的英语 – colymore 2012-01-09 00:03:21

1

试试这个

public boolean onKeyDown(int keyCode, KeyEvent msg) { 
     if ((keyCode == KeyEvent.KEYCODE_BACK)) { 
      //Add Dest View 
      finish(); 

      return false; 
     } 
     else { 
      return true; 
     } 
    } 
1

尝试使用

finishAffinity(); 

insted的的

finish(); 
+0

这对我的作品感谢^^ – 2017-05-11 23:26:22

+0

@HeshamMorsy不客气。如果真的有帮助,你可以提高答案。 – 2017-05-22 09:47:23

+0

当然我upvoted的答案:)) – 2017-05-22 13:29:37