0

I have a ViewPager with two fragments. What I want to do;

Change picture of a ImageView and change some properties of a TextView from an another fragment.

I tried this to do:

This is in my SettingsFragment.java. From this fragment I want to change some views of the CalculatorFragment. The OnActivityResult method is called when the user picks a picture from the gallery. If this method is called I am saving this picture and then call the 'callback method' so the MainActivity knows that the user picks a new picture from the gallery.

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == getActivity().RESULT_OK) {
        if (requestCode == SELECT_PICTURE) {

            try {
                imageSaver.save(MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), data.getData()));
            } catch (IOException e) {
                e.printStackTrace();
            }

            settingsFragmentCallback.newMessage(NEW_PICTURE);
        }
    }
}

public interface SettingsFragmentCallback{
    void newMessage(String message);
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    try {
        settingsFragmentCallback = (SettingsFragmentCallback) context;
    } catch (ClassCastException e){

    }
}

This is in my MainActivity. Here I receive the message from the Settings Fragment and when I receive it I call the Calculator Fragment getMessage method, so this Fragment knows that the user picks a picture from the gallery.

public class MainActivity extends FragmentActivity implements
                                   SettingsFragment.SettingsFragmentCallback {

@Override
public void newMessage(String message) {
    CalculatorFragment cf = (CalculatorFragment) 
                 getSupportFragmentManager().findFragmentById(R.id.fragmentCalculator);
    cf.getMessage(message);
}

This is in the Calculator Fragment. If the user picked a new picture from gallery, it calls the setbackground method.

private void setBackground(){
    final Bitmap bitmap = imageSaver.load();

    if(bitmap != null){
        imageViewBackground.setImageBitmap(bitmap);

    }
}

public void getMessage(String message){
    switch (message){
        case SettingsFragment.NEW_PICTURE:
            setBackground();
            break;
        default:
            break;
    }
}

I debugged this and it works fine (it comes in the setBackground method) but for some reason the ImageView is not updated. The image itself is saved en loaded correctly, but the ImageView is simply not updated.

Thanks for help!

tomhogenkamp
  • 83
  • 1
  • 11

2 Answers2

0

Since both fragments are in same Activity, add a method to Activity class and call it from one fragment when you want to change something inside the other fragment. Before touching UI, make sure and check the fragment on which you want to make changes is "added" to the activity by calling its isAdded() method.

Ugurcan Yildirim
  • 5,973
  • 3
  • 42
  • 73
0

Suppose you have two fragments FragmentA and Fragment B. Now FragmentA has some ImageView and TextView and you want to change from FragmentB. Suppose in ViewPager FragmentA is at position 0 and FragmentB is at position 1. Now when you want to change data do as following:

View view = yourViewPager.getChildAt(0);
((ImageView) view.findViewById(R.id.idOfImageView)).setImageResource(yourResource);
((TextView) view.findViewById(R.id.idOfTextView)).setText("Your Text");
Megha Maniar
  • 444
  • 5
  • 22