簡體   English   中英

“返回按鈕”不會關閉活動

[英]“Back button” doesn't close activity

我的申請中有兩項活動。

第一個是地圖,其中將來自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);
      }

這是我在onTap方法中使用的getLugarPorID方法:

/*********************************************
         * 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;

        }

它應該是

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

嘗試使用

finishAffinity(); 

裝的

finish();

嘗試這個

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

            return false;
        }
        else {
            return true;
        }
    }

暫無
暫無

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

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