When I start my app I show a splashscreen where I show my logo and in the background I'm getting the GPS coordinations (latitude and longitude). Afterwards it goes to my MainFragmentActivity where I set my viewpager, FragmentManager and MyFragmentPagerAdapter. I set the fragments up in MyFragmentPagerAdapter. And that is my problem, I can't seem to get the GPS coordinations to my fragment.
A brief summary:
GPS coordinations are calculated in the splashscreen, after splashscreen is done MainFragmentActivity opens up and everything is being setted up so my viewpager works. Then I want to be able to access those GPS coordinations that are calculated in my splashscreen in my fragments. I tried passing them by extra, but my fragment isn't being opened by an intent startActivty
. So I'm stuck here.
SplashScreen
//Calculating GPS coordinations ...
CountDown tik;
tik = new CountDown(3000, 1000, this, MainActivity.class);
tik.start();
StartAnimations();
CountDown
//After SplashScreen is done, start MainActivity
public class CountDown extends CountDownTimer{
private Activity myActivity;
private Class myClass;
public CountDown(long millisInFuture, long countDownInterval, Activity act, Class cls) {
super(millisInFuture, countDownInterval);
myActivity = act;
myClass = cls;
}
@Override
public void onFinish() {
myActivity.startActivity(new Intent(myActivity, myClass));
myActivity.finish();
}
@Override
public void onTick(long millisUntilFinished) {}
}
MainActivity
//Setting viewpager up
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Tried getting coordinations like this and then passing it to fragment, but didn't succeed.
/*Bundle extras = getIntent().getExtras();
myLat = Double.toString(extras.getDouble("lat"));
myLong = Double.toString(extras.getDouble("lng"));*/
/** Getting a reference to the ViewPager defined the layout file */
final ViewPager pager = (ViewPager) findViewById(R.id.pager);
/** Getting fragment manager */
FragmentManager fm = getSupportFragmentManager();
/** Instantiating FragmentPagerAdapter */
MyFragmentPagerAdapter pagerAdapter = new MyFragmentPagerAdapter(fm);
/** Setting the pagerAdapter to the pager object */
pager.setAdapter(pagerAdapter);
pager.setPageTransformer(true, new ZoomOutPageTransformer());
PageListener pagelistener = new PageListener();
pager.setOnPageChangeListener(pagelistener);
}
MyFragmentPagerAdapter
public class MyFragmentPagerAdapter extends FragmentPagerAdapter{
final int PAGE_COUNT = 6;
/** Constructor of the class */
public MyFragmentPagerAdapter(FragmentManager fm) {
super(fm);
}
/** This method will be invoked when a page is requested to create */
@Override
public Fragment getItem(int arg0) {
switch(arg0){
case 0:
return new FragmentOne();
case 1:
return new FragmentTwo();
case 2:
return new FragmentThree();
case 3:
return new FragmentFour();
case 4:
return new FragmentFive();
case 5:
return new SettingsFragment();
default:
return null;
}
}
/** Returns the number of pages */
@Override
public int getCount() {
return PAGE_COUNT;
}
}
**Example of a Fragment, in this instance I just used FragmentOne
public class FragmentOneextends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
//Want to be able to access those coordinations here.
View v = inflater.inflate(R.layout.frag_one, container, false);
}
}
LocationHelper
public class LocationHelper extends Activity{
private static LocationHelper mInstance = new LocationHelper();
private LatLng mLocation;
public static double location_latitude;
public static double location_longitude;
LocationManager locationManager;
LocationListener locationListener;
private LocationHelper() {
super();
// start getting location
// Acquire a reference to the system Location Manager
locationManager = (LocationManager) this
.getSystemService(Context.LOCATION_SERVICE);
//If I remove 'extends Activity, then I get this error: The method getSystemService(String) is undefined for the type LocationHelper
// Define a listener that responds to location updates
locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location
// provider.
location_latitude = location.getLatitude();
location_longitude = location.getLongitude();
Log.v("Location", "set" + location.getLatitude());
if (location.getLatitude() != 0.0) {
Log.v("Location", "stop looking");
locationManager.removeUpdates(locationListener);
FileOutputStream fos;
try {
//Getting the same error here The method openFileOutput(String, int) is undefined for the type new LocationListener(){}
fos = openFileOutput("my_latitude", Context.MODE_PRIVATE);
fos.write((""+location_latitude).getBytes());
fos.close();
fos = openFileOutput("my_longitude", Context.MODE_PRIVATE);
fos.write((""+location_longitude).getBytes());
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
} else {
Log.v("Location", "keep looking");
}
}
public void onStatusChanged(String provider, int status,
Bundle extras) {
Log.v("Location", provider + ", " + status + " Status changed");
}
public void onProviderEnabled(String provider) {
Log.v("Location", provider + " onProviderEnabled");
}
public void onProviderDisabled(String provider) {
Log.v("Location", provider + " onProviderDisabled");
}
};
}
public static LocationHelper getInstance() {
return mInstance;
}
public LatLng getLocation() {
return mLocation;
}
}