1. 程式人生 > >在fragment中獲取Application資料

在fragment中獲取Application資料

publicclassMyApplicationextendsApplication{publicint getColorCount(){return1;}}

publicclassTestFragmentextendsFragment{@OverridepublicvoidonActivityCreated(Bundle savedInstanceState){super.onActivityCreated(savedInstanceState);int test =((MyApplication) getActivity().getApplication()).getColorCount();
Log.d("testfrag",""+ test);}
Make sure you only try to access Activity during or after onActivityCreated