0

In my app I use Fragments to show some location based data. The information of the current location of the user is fetched in my MainActivity and the Fragment uses these coordinates to proceed. The question I have is, how I let these two classes work together: as I use it now, the Fragment tries to access the location data in the MainActivity as soon as it‘s opened, regardless whether the location was already fetched or not. How can I fetch the location and notify the Fragment afterwards so it knows that it can proceed with its code and access the coordinates properly? Or is the whole procedure thought wrong and that kind of issue is handled differently?

This is the idea of my MainActivity:

public class MainActivity extends AppCompatActivity {

public static Double lat;
public static Double lon;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    showLocation();

}

private void showLocation() {
//Grab Location
    if (location != null) {
        lat = location.getLatitude();
        lon = location.getLongitude();
    }

}

This is the idea of my Fragment:

public class FragmentA extends Fragment {

public FragmentA() {
    // Required empty public constructor
}

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

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_a, container, false);


    return view;
}

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

}

public void start(){
    if (MainActivity.lat != null && MainActivity.lon != null) {
        lat = MainActivity.lat;
        lon = MainActivity.lon;

        //proceed with location data
    } else {

    }

}

}
user9582784
  • 175
  • 1
  • 8
  • 1
    you could try looking into LiveData from ViewModels here: https://developer.android.com/topic/libraries/architecture/livedata. I also found this example on Medium to be super helpful understanding it in an application example: https://medium.com/androiddevelopers/viewmodels-a-simple-example-ed5ac416317e – Barcode Mar 25 '19 at 19:07

1 Answers1

1

You can find from the Activity the Fragment you want to access.

You can look for it inside the container (xml) where it is inflated:

((FragmentA) getSupportFragmentManager().findFragmentById(R.id.yourContainer))

or

You can search for the fragment within the stack through the tag assigned at the time of loading:

// First load your fragment
getSupportFragmentManager().beginTransaction()
  .replace(R.id.yourContainer, fragment, "yourFragmentTag")
  .addToBackStack(null)
  .commit()

// Then you can access it
((FragmentA) getSupportFragmentManager().findFragmentByTag("yourFragmentTag"))

To access from a Fragment to an Activity, you can use an interface, as discussed in this answer.

Good luck!

Jose Angel Maneiro
  • 1,226
  • 9
  • 20
  • Thank you so far, but do you also have a suggestion if I‘m using a ViewPager? – user9582784 Mar 26 '19 at 18:45
  • 1
    It's very similar. From the Fragment, you will still have a valid instance of the Activity, so you will be able to communicate with it through an interface, as I tell you above. On the other hand, in a ViewPager you can overwrite the getItem() method of your FragmentPagerAdapter so that it returns the current Fragment. – Jose Angel Maneiro Mar 27 '19 at 08:00