android - Fragment findviewbyid not working correctly -
i have tablet layout , phone layout , need tablet detect whether view exists determine whether execute tablet code or phone code.
this code in activity fragment attached returns true, same code in fragment returns false.
mtwopane = view.findviewbyid(r.id.detail_container) != null;
what happening causing this? i've tried several things such mtwopane = getactivity().getrootview().findviewbyid(r.id.detail_container) != null;
no luck.
my activity_main.xml phone:
<fragment xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/mainfragment" android:name="com.project.mainfragment" tools:context="com.project.mainfragment"/>
my activity_main tablet:
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:baselinealigned="false" android:divider="?android:attr/dividerhorizontal" android:orientation="horizontal" tools:context="com.project.mainactivity"> <fragment android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" android:id="@+id/mainfragment" android:name="com.project.mainfragment" /> <framelayout android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" android:id="@+id/detail_container"/> </linearlayout>
my mainactivity's oncreate:
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); if (findviewbyid(r.id.detail_container) != null) { mtwopane = true; if (savedinstancestate == null) { getsupportfragmentmanager() .begintransaction() .replace(r.id.detail_container, new detailfragment(), detailfragment_tag) .commit(); } } else { mtwopane = false; } log.e(tag, boolean.tostring(mtwopane)); }
my fragment's oncreateview:
@override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view view = inflater.inflate(r.layout.fragment_main, container, false); mtwopane = view.findviewbyid(r.id.detail_container) != null; log.e(tag, boolean.tostring(mtwopane)); return view; }
Comments
Post a Comment