Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static Fragment newInstance() {
- final Bundle bundle = new Bundle();
- ...
- final Fragment fragment = new MyFragment();
- fragment.setArguments(bundle);
- return fragment;
- }
- public interface MyDependencies {
- MyDep getDep();
- }
- public MyActivity extends Activity implements MyDependencies {
- @Override
- public MyDep getDep(){
- return createMyDependencyOrGetItFromASingletonOrGetItFromApplication()
- }
- }
- public void onActivityCreated(Bundle b){
- super.onActivityCreated(b)
- if (getActivity() instanceOf MyDependencies) {
- MyDep dep = ((MyDependencies) getActivity).getDep();
- } else {
- throw new RuntimeException("Context does not support the Fragment, implement MyDependencies")
- }
- }
- public void onActivityCreated( Bundle b){
- super.onActivityCreated(b)
- DependencyClass c = ((MyActivity)getActivity()).getDependency();
- }
- Fragment MyFragment {
- Depend mDepend;
- ....
- public void setDepend(Depend depend) {
- mDepend = depend;
- }
- }
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_map);
- MapFragment wrapperFragment = new WrapperFragment();
- if (savedInstanceState == null) {
- getFragmentManager().beginTransaction()
- .add(R.id.map_container, wrapperFragment).commit();
- // find the fragment
- // call the setter
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement