I want to parcel an array
with a fragment. The parcelable array depends on firestore data retrieving. I mean the elements of array are got from Firestore. But retrieving data from Firestore is geting very late and and the next lines of code are being executed as well as a null array is being parceled. What to do to make the next lines wait until the data is retrieved from the Firestore??
public class MainActivity extends AppCompatActivity implements OnMapReadyCallback {
private static final String MAPVIEW_BUNDLE_KEY = "MapViewBundleKey";
private static final int PERMISSIONS_REQUEST_ENABLE_GPS = 9001;
private static final int PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION = 9002;
private static final String TAG = "MainActivity";
private static final int ERROR_DIALOG_REQUEST = 9003;
private boolean mLocationPermissionGranted = false;
private List<User>mUserList=new ArrayList<>();
private ArrayList<UserLocation>mUserLocations=new ArrayList<>();
private FusedLocationProviderClient mFusedLocationProviderClient;
FirebaseFirestore mDb;
private GoogleMap mGoogleMap;
private UserLocation mUserPosition=null;
private LatLngBounds latLngBoundary;
private ClusterManager mClusterManager;
private MyClusterManagerRenderer mClusterManagerRenderer;
private ArrayList<ClusterMarker> mClusterMarkers=new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDb = FirebaseFirestore.getInstance();
initUser();//in this method the data retrieving is implemented
if (findViewById(R.id.fragment_container) != null) {
if (savedInstanceState != null) {
return;
}
MapFragment mapFragment = new MapFragment();
Bundle bundle=new Bundle();
bundle.putParcelableArrayList(getString(R.string.userlocations_array), mUserLocations);
mapFragment.setArguments(bundle);
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, mapFragment).commit();
}
}
private void initUser() {
User user=new User();
user.setEmail("nobeld@gmail.com");
user.setResponse("ok");
user.setUser("student");
user.setUserId("5");
user.setUserName("nobel");
((UserClient)(getApplicationContext())).setUser(user);
mUserList.add(user);
User user1=new User();
user1.setEmail("rahuld@gmail.com");
user1.setResponse("ok");
user1.setUser("student");
user1.setUserId("6");
user1.setUserName("rahul");
User user2=new User();
user2.setEmail("milond@gmail.com");
user2.setResponse("ok");
user2.setUser("student");
user2.setUserId("7");
user2.setUserName("milon");
mUserList.add(user1);
mUserList.add(user2);
for(User u: mUserList){
getUserLocation(u);
//firestore is implemented inside this method
Log.d(TAG, "initUser: in user array");
}
}
private void setCameraView(){
if(mUserPosition!= null){
Log.d(TAG, "setCameraView: user position got");
double bottomboundary=mUserPosition.getGeo_point().getLatitude()-.05;
double leftboundary = mUserPosition.getGeo_point().getLongitude()-.05;
double upboundary = mUserPosition.getGeo_point().getLatitude()+.05;
double rightboundary = mUserPosition.getGeo_point().getLongitude()+.05;
latLngBoundary=new LatLngBounds(new LatLng(bottomboundary,leftboundary),
new LatLng(upboundary,rightboundary));
mGoogleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(latLngBoundary,0));
}else {
Log.d(TAG, "setCameraView: user position is null");
}
}
private void getUserLocation(User user){
Log.d(TAG, "getUserLocation: ");
DocumentReference locationRef=mDb.collection(getString(R.string.collection_user_location_student))
.document(user.getUserId());
locationRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if(task.isSuccessful()){
if(task.getResult().toObject(UserLocation.class)!= null){
Log.d(TAG, "Location onComplete: ");
UserLocation u=task.getResult().toObject(UserLocation.class);
mUserLocations.add(u);
//here adding the elements to array.
}else {
Log.d(TAG, "onComplete: result is empty");
}
}
}
});
}
}