在startActivityForResult之后没有调用onCreate和onRestoreInstanceState(Bundle)

时间:2014-06-08 20:02:54

标签: android android-intent oncreate onactivityresult

我正在尝试从图库中加载图像 这是onCreate()

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_gallery_picture);

    mThis = this;
    mImageCache = new TakingPictureActivityCacheMngr(this);

    initUi();

    if (savedInstanceState == null) {
        openGallery();
    }
}

以下是onSaveInstanceState()

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    // save file url in bundle as it will be null on scren orientation
    // changes
    outState.putParcelable("file_uri", mFileUri);
    if (com.isee.spot.toolkit.Config.IS_DEBUG) {
        Log.d(TAG, "Instance was saved.");
    }
}

恢复时:(从不打电话)

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);

    // get the file Uri
    mFileUri = savedInstanceState.getParcelable("file_uri");
}

在打开图库后永远不会调用onCreate(),在图库打开时会调用onDestroy(),因此onSaveInstanceState()会调用<activity android:name=".GalleryPictureActivity" android:configChanges="orientation|keyboard|keyboardHidden" android:screenOrientation="portrait" > </activity>

以下是清单中的活动声明:

onActivityResult()

我重载了@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); // if the result is loading image from gallery if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) { ...

{{1}}

2 个答案:

答案 0 :(得分:1)

这是答案, 活动本身是正确的 我使用的问题是启动它的活动:galleryPic.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); 显然这会导致onActivityResult(int, int, Intent)无法被调用 留下这只是为了让某人有同样的行为。 谢谢你试图提供帮助,我猜你没有机会。

答案 1 :(得分:0)

我可能无法正确理解此代码的上下文,但看起来这是一个自定义图像选择类?你可能知道 - 但可能不是 - 有内置的SDK方法...见下文:

private void pickImage() {
    Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
    photoPickerIntent.setType("image/*");
    startActivityForResult(photoPickerIntent, REQUEST_PICK_IMAGE);
}

@Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
    switch (requestCode) {
        case REQUEST_PICK_IMAGE:
            if (resultCode == Activity.RESULT_OK) {
                Log.v(TAG, "User Picked An Image");
                handleImage(data.getData());
            } 
            else if (resultCode == Activity.RESULT_CANCELED) { 
               Log.v(TAG, "User Cancelled Pick Image");
            }
            break;

        default:
            super.onActivityResult(requestCode, resultCode, data);
            break;
    }
}

private void handleImage(final Uri selectedImage) {
    String filePath = selectedImage.getEncodedPath();
    Log.v(TAG, " -- picked image is " + filePath);
    MyImageView.setImage(selectedImage);
}