android - Getting application context into fragment, ViewPager -
i'm writing simple app swipe view using viewpager
, fullscreen
fragments. in 1 of fragments want check internet connection status before displaying data can't application context. i've found lot of topics on issue, none of solutions works me. using getapplication()
within fragment though error "unreacheable statement"
.
any advice?
import android.support.v4.app.fragmentactivity; import android.support.v4.view.viewpager; import android.os.bundle; public class mainactivity extends fragmentactivity { viewpager viewpager; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); //setting fragment view pager viewpager = (viewpager)findviewbyid(r.id.pager); pageradapter pageradapter = new pageradapter(getsupportfragmentmanager()); viewpager.setadapter(pageradapter); } }
activity_main.xml
<android.support.v4.view.viewpager xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="match_parent"> </android.support.v4.view.viewpager>
first fragment
import android.support.v4.app.fragment; import android.os.bundle; import android.support.annotation.nullable; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; public class currentweatherfragment extends fragment { //flaga dla statusu polaczenia z internetem boolean isinternetpresent = false; //klasa detektora połączenia connectiondetector cd; @nullable @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view v = inflater.inflate(r.layout.currentweather, container, false); return v; //tworzenie klasy detektora cd = new connectiondetector(getactivity().getapplicationcontext()); //sprawdzenie czy jest polaczenie z internetem isinternetpresent = cd.isconnectingtointernet(); } }
pager adapter class
import android.support.v4.app.fragment; import android.support.v4.app.fragmentmanager; import android.support.v4.app.fragmentpageradapter; public class pageradapter extends fragmentpageradapter { public pageradapter(fragmentmanager fm) { super(fm); } @override public fragment getitem(int arg0) { switch (arg0){ case 0: return new currentweatherfragment(); case 1: return new forecastfragment(); default: break; } return null; } @override public int getcount() { return 2; } }
in currentweatherfragment
's oncreateview
inflate view
instantly return it.
public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view v = inflater.inflate(r.layout.currentweather, container, false); return v; //tworzenie klasy detektora cd = new connectiondetector(getactivity().getapplicationcontext()); //sprawdzenie czy jest polaczenie z internetem isinternetpresent = cd.isconnectingtointernet(); }
move return v;
end of method:
public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view v = inflater.inflate(r.layout.currentweather, container, false); //tworzenie klasy detektora cd = new connectiondetector(getactivity().getapplicationcontext()); //sprawdzenie czy jest polaczenie z internetem isinternetpresent = cd.isconnectingtointernet(); return v; }
Comments
Post a Comment