package com.lukekorth.android_500px;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;

import com.lukekorth.android_500px.models.Photos;

import it.sephiroth.android.library.imagezoom.ImageViewTouch;

import static android.os.Build.VERSION.SDK_INT;
import static android.os.Build.VERSION_CODES.JELLY_BEAN;

public class PhotoFragment extends Fragment {

    public static final String PHOTO_ID_KEY = "com.lukekorth.android_500px.PhotoFragment.PHOTO_ID_KEY";

    private Photos mPhoto;

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.full_screen_photo, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        ViewTreeObserver viewTreeObserver = getView().getViewTreeObserver();
        if (viewTreeObserver.isAlive()) {
            viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    if (SDK_INT >= JELLY_BEAN) {
                        getView().getViewTreeObserver().removeOnGlobalLayoutListener(this);
                    }

                    if (mPhoto == null) {
                        mPhoto = Photos.getPhoto(getArguments().getInt(PHOTO_ID_KEY));

                        ImageViewTouch photoView = (ImageViewTouch) getView().findViewById(R.id.photo);

                        WallpaperApplication.getPicasso(getActivity())
                                .load(mPhoto.imageUrl)
                                .resize(photoView.getWidth(), photoView.getHeight())
                                .centerInside()
                                .into(photoView);
                    }
                }
            });
        }
    }

}

@@ -671,16 +671,237 @@
  mPhoto;
+%0A    private boolean mPhotoSet;%0A%0A    @Override%0A    public void onCreate(Bundle savedInstanceState) %7B%0A        super.onCreate(savedInstanceState);%0A        mPhoto = Photos.getPhoto(getArguments().getInt(PHOTO_ID_KEY));%0A    %7D
 %0A%0A    @O
@@ -1183,114 +1183,71 @@
 d on
-ActivityCreated(@Nullable Bundle savedInstanceState) %7B%0A        super.onActivityCreated(savedInstanceState)
+Resume() %7B%0A        super.onResume();%0A%0A        mPhotoSet = false
 ;%0A%0A 
@@ -1253,16 +1253,22 @@
         
+final 
 ViewTree
@@ -1619,30 +1619,17 @@
         
-getView().getV
+v
 iewTreeO
@@ -1635,18 +1635,16 @@
 Observer
-()
 .removeO
@@ -1723,113 +1723,21 @@
 if (
+!
 mPhoto
- == null) %7B%0A                        mPhoto = Photos.getPhoto(getArguments().getInt(PHOTO_ID_KEY));%0A
+Set) %7B
 %0A   
@@ -1837,17 +1837,16 @@
 photo);%0A
-%0A
         
@@ -2146,16 +2146,58 @@
 oView);%0A
+                        mPhotoSet = true;%0A
         

Fix missing photos after paging through several