簡體   English   中英

如何在Android中按后退按鈕返回上一屏幕

[英]How do I go back to previous screen on pressing back button in Android

單擊屏幕上的按鈕時,將打開相機。 我看到關於這個問題的堆棧溢出的帖子很少。 我在dispatchTakePictureIntent();之后嘗試了finish()finishActivity()onBackPressed()方法dispatchTakePictureIntent(); onClick方法中但失敗了! 退出了我的整個應用程序。

這是我完整代碼的摘錄:

按鈕的onClick方法:

 mCaptureImage.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
          Log.d("TAG", "Camera opened");
          dispatchTakePictureIntent();
      }
  });

此方法分派 Intent:

private void dispatchTakePictureIntent() {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        // Ensure that there's a camera activity to handle the intent
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
            // Create the File where the photo should go
            File photoFile = null;
            try {
                photoFile = createImageFile();
            } catch (IOException ex) {
                // Error occurred while creating the File
            }
            // Continue only if the File was successfully created
            if (photoFile != null) {
                photoURI = FileProvider.getUriForFile(this,
                        "com.pc.android.fileprovider",
                        photoFile);
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
                startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
            }
        }
    }

我的問題是,當我處於 Camera's Intent 狀態時按下后退按鈕時,如何返回上一個屏幕。

Thus you have to do Nothing :

您不能覆蓋您正在調用的外部活動中的方法。 但是,當用戶在使用 startActivityForResult 調用的活動中回擊時,通常會返回響應代碼 RESULT_CANCELLED(在某些情況下可能並非如此)。 在您的 onActivityResult 方法中,只需檢查 RESULT_CANCELLED 代碼並調用您需要的任何功能

您可以嘗試使用 startActivityForResult 並檢查您的 onActivityResult 是否被調用或在后按 Activity 的 onResume 被調用

暫無
暫無

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

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