Issue
I have a App with a Navigation Drawer and some fragments.
In my MainActivity i get the actual lat & long Location of the device.
On my first Fragment in the drawer i load in a AsyncTask some stuff out of my Database on my Webserver
For this request i need my Lat & Long variables from the MainActivity.
But how can i call a getLag() Method from MainActivity out of my AsyncTask or get my 2 variables?
I need the variables in the Methode updateJSONdata which is called in doInBackround()
public class LoadEvents extends AsyncTask<Void, Void, Boolean>{
Activity activity;
ListFragment LF;
String TAG;
public LoadEvents(Activity A, ListFragment lf, String TAG) {
this.TAG = TAG;
this.LF = lf;
this.activity = A;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
MySwipeRefreshLayout srl = (MySwipeRefreshLayout) LF.getView().findViewById(R.id.srl);
if(srl!=null){
srl.setRefreshing(true);
}
}
@Override
protected Boolean doInBackground(Void... arg0) {
updateJSONdata(TAG, activity);
return null;
}
Solution
Fragment onAttach()
MainActivity mainActivity;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
mainActivity = (MainActivity) activity;
}
Calling Activity method like this in your fragment
if (mainActivity != null)
latLng = mainActivity.getLatLng(); //getLatLng() is a method defined in MainActivity
Answered By - Bharatesh
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.