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!