简体   繁体   中英

New activity from intent

I have fragment with button with intent for Gallery

@Override
public void onClick(View v) {
    switch (v.getId()){
        //если выбрали галерею
        case R.id.b_gallery:
            Intent intentGallery = new Intent(Intent.ACTION_PICK);
            intentGallery.setType("image/*");
            startActivityForResult(intentGallery, GALLERY_INTENT_REQUEST_CODE);
        break;

Then I choose any photo and receive URI. I have to show chosen photo in new activity. In fragment i do:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent imageReturned) {
    super.onActivityResult(requestCode, resultCode, imageReturned);
    switch (requestCode) {

        //если результат пришел от галереи
           case GALLERY_INTENT_REQUEST_CODE:
            if (resultCode == RESULT_OK) {
                Uri selectedImage = imageReturned.getData();
                try {
                    bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), selectedImage);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                Intent last_intent_gallery = new Intent(getView().getContext(), ViewPhoto.class);
                last_intent_gallery.putExtra("fotka", bitmap);
                startActivity(last_intent_gallery);
            }
            break;

In new activity i do:

public class ViewPhoto extends Activity {

    //public final static String THIEF = ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view_photo_layout);

        CustomImageVIew imageView = (CustomImageVIew)findViewById(R.id.customImageVIew);

        Intent intent = getIntent();
        Bitmap fotka = (Bitmap) intent.getExtras().get("fotka");
        imageView.setBitmap(fotka);

    }

But after i choose photo Gallery just close and i again get my fragment. Where is my decision?

My AndroidManifest has:

<activity android:name=".fragments.ViewPhoto"
    android:configChanges="orientation|screenSize">
    <intent-filter>
        <action android:name="android.intent.action.DEFAULT"/>
    </intent-filter>
</activity>

Request codes for your starting activity for result and getting result are not the same constant. See the names of the constants. One has "photo" in it. One has "gallery". Use same code for requesting and getting result.

Edit: I see your edit now based on your comment below. See if your call to onActivityResult is not intercepted by activity. It usually is. In that case in your activity forward the call to your fragment method. Or just override the method in activity so it implements the super. Which will call the fragment method. It's a common issue with starting activity for result with fragment. Do some logging. See if it calls the method in fragment. My guess is.. it doesn't. That's why no action is taken.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM