0

I have a question regarding the passing of variables to my Fragment from the MainActivity.

In my MainActivity I have a button, which creates and displays a fragment respectively reloads it, if it is already created. (I am using this code to reload the fragment) I am passing 2 double variables to it via the Bundle-class. This works when I am first creating the fragment, but how can I update the variables, when I want to reload the fragment? In my code, I am not creating a new Fragment, but detach and reattach it. Is this possible with my approach or do I need something completely different?

MainActivity

public void loadWeatherFragment(){

    longitude = this.getLongitude();
    latitude = this.getLatitude();
    FragmentManager fm = getFragmentManager();
    Fragment currentFragment = this.getFragmentManager().findFragmentById(R.id.fragment_container);


    //If the fragment already exists, reload it
    if (currentFragment instanceof FragmentWeather) {
        FragmentTransaction fragTransaction =   this.getFragmentManager().beginTransaction();
        fragTransaction.detach(currentFragment);
        fragTransaction.attach(currentFragment);
        fragTransaction.commit();
    }
    //Else create the fragment
    else{
        FragmentWeather mFragmentWeather = new FragmentWeather();
        Bundle args = new Bundle();
        args.putDouble("longitude", longitude);
        args.putDouble("latitude", latitude);
        mFragmentWeather.setArguments(args);
        FragmentTransaction ft = fm.beginTransaction();
        ft.add(R.id.fragment_container, mFragmentWeather);
        ft.commit();
    }
}

FragmentWeather

public class FragmentWeather extends Fragment {


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        View view = inflater.inflate(R.layout.fragment_weather, container, false);

        Double latitude = this.getArguments().getDouble("latitude");
        Double longitude = this.getArguments().getDouble("longitude");

    }

}
Community
  • 1
  • 1
Michael B
  • 1,660
  • 3
  • 28
  • 59

2 Answers2

1

Save the variables in your Fragment class like in the example below. Also note how I changed from creating variables latitude and longitude in the onCreateView() to simply saving a value to them. Now you can access them inside the fragment at anytime.

public class FragmentWeather extends Fragment {

private Double latitude;
private Double longitude;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    View view = inflater.inflate(R.layout.fragment_weather, container, false);

    latitude = this.getArguments().getDouble("latitude");
    longitude = this.getArguments().getDouble("longitude");

}
}

If you need the update these variables from other classes, create getters and setters for the variables (many IDEs can also do this for you, in Windows Android Studio Alt + Insert is the hotkey). Then as long as a class can see your Fragment, it can access these variables too. Define

public Double getLongitude()
{
    return this.longitude;
}

public void setLongitude(double val)
{
    this.longitude = val;
}

and to use them

FragmentWeather fragment = new FragmentWeather();
double longitude = 0.0;
fragment.setLongitude(longitude);
double latitude = fragment.getLatitude();
milez
  • 2,201
  • 12
  • 31
  • Hey. First I didn't understand what you meant. But now I get it. If I make a setter-method in the fragment, I can call this method from my MainActivity to update my Data. `currentFragment.setLatitude(99.99)` Works! Thank you! – Michael B Jul 20 '15 at 14:40
  • Great! I'll edit the answer to be more clear if someone in the future reads it. – milez Jul 20 '15 at 14:42
0

Is there any reason, why the fragment has to be unloaded and then reloded?

The most simple solution is to give the fragment a public api that can be called from the activity:

public class FragmentWeather extends Fragment {
    //...
    // public api to be used by owning activity
    public setLatLon(double latitude, double longitude) {
        // do what is neccessary to show lat/lon
    }
}

public class MainActivity ... {
    public void loadWeatherFragment(){

        Fragment currentFragment = this.getFragmentManager().findFragmentById(R.id.fragment_container);

        //If the fragment already exists, reload it
        if (currentFragment instanceof FragmentWeather) {
            ((FragmentWeather) currentFragment).setLatLon(...)
        }
    }       
}
k3b
  • 14,517
  • 7
  • 53
  • 85