Fringo
Fringo

Reputation: 313

The second instance of the fragment is acting weird

Inside my project i have a fragment that launches a fragment. When i first run my application and visit the app, everything works really well. but when i hit the back key and return to the first fragment, and then tap and go to the second fragment.. Well everything goes blank!

Here's how i launch the second fragment from the first one:

ft.addToBackStack(null);
ft.replace(R.id.content_frame, UDP);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();

I have an ImageView in the second fragment, when i launch it first it works great, but at the second time, they image displays nothing!

Here's the code for the second fragment:

public UniversityImage newInstance() {
        UniversityImage Instance = new UniversityImage();
        return Instance;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View viewta = inflater.inflate(R.layout.universityimage, container,
                false);
        prefs = this.getSherlockActivity().getSharedPreferences(prefName,
                Context.MODE_PRIVATE);
        setRetainInstance(false);
        uniiv = (ImageView) viewta.findViewById(R.id.ivUNI);
        Log.e("Uni", prefs.getString("UniversityName", "LAU"));
        ILoader.DisplayImage(c.getString(c
                .getColumnIndex(UniversitiesDatabase.KEY_IMAGEURL)),uniiv);
 return viewta;
    }
}

Even when i first launch my Fragment, it logs everything, the second time it logs nothing! what am i doing wrong? Thank you guys

Upvotes: 1

Views: 41

Answers (1)

Lena Bru
Lena Bru

Reputation: 13937

put this code

 prefs = this.getSherlockActivity().getSharedPreferences(prefName,
                Context.MODE_PRIVATE);
        setRetainInstance(false);
        uniiv = (ImageView) viewta.findViewById(R.id.ivUNI);
        Log.e("Uni", prefs.getString("UniversityName", "LAU"));
        ILoader.DisplayImage(c.getString(c
                .getColumnIndex(UniversitiesDatabase.KEY_IMAGEURL)),uniiv);

in

onActivityCreated(Bundle savedInstanceState){

  super.onActivityCreated(savedInstanceState);
   prefs = this.getSherlockActivity().getSharedPreferences(prefName,
                Context.MODE_PRIVATE);

        uniiv = (ImageView) getView().findViewById(R.id.ivUNI);
        Log.e("Uni", prefs.getString("UniversityName", "LAU"));
        ILoader.DisplayImage(c.getString(c
                .getColumnIndex(UniversitiesDatabase.KEY_IMAGEURL)),uniiv);

}

Upvotes: 1

Related Questions