Joey Yi Zhao
Joey Yi Zhao

Reputation: 42604

startActivityForResult doesn't return to the same instance of Activity

I am using below code to take a photo from camera. I use startActivityForResult for the ACTION_IMAGE_CAPTURE intent in my CameraActivity class. The camera can be open and it works fine to take a photo. But when it comes back to the CameraActivity, it goes into a different instance of CameraActivity. I am printing the hascode value in these two methods and it gives a different value. I don't understand why it creates two activities. Does anyone know this error?

public class CameraActivity  extends Activity{

private ImageView imageView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if(savedInstanceState == null) {
        setContentView(R.layout.fragment_imageview);
        imageView = (ImageView)findViewById(R.id.imageview);
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        System.out.println(this.hashCode());
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
            startActivityForResult(takePictureIntent, 2);
        }
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode == 2 && resultCode == RESULT_OK){
        System.out.println(this.hashCode());
        Bundle args = data.getExtras();
        Bitmap bitmap = (Bitmap)args.get("data");
        imageView.setImageBitmap(bitmap);

    }
   }
 }

Upvotes: 0

Views: 266

Answers (1)

marcinj
marcinj

Reputation: 50036

It is possible that system is destroying your activity for the time the camera application is working. When camera application is started, your app is in background so it is eligible for being destroyed. You can test it by adding logging to your onDestroy override.

Upvotes: 3

Related Questions