I was able to access the hardcoded pre-instantiated fragment but the cast fails. Trying to call setArguments
or call specific methods on the class.
to be able to call Fragment.setArguments
on findViewById(R.id.product);
can solve my problem though
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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">
<fragment
android:id="@+id/product"
android:name="Product"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout="@layout/product_fragment" />
</android.support.constraint.ConstraintLayout>
MainActivity
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(savedInstanceState == null) {
findViewById(R.id.product); // works
findViewById(R.id.product).findViewById(R.id.subview); // works
Product frag = (Product) findViewById(R.id.product); // cast fails
}
}
}
Product
public class Product extends Fragment {
// implementation
}