android - How to use the fragment's getActivity() method to pass a context? -
i'm working on android app, , gave me these errors
avoid non-default constructors in fragments: use default constructor plus fragment#setarguments(bundle) instead
and
this fragment should provide default constructor (a public constructor no arguments)
this code:
public afragment newinstance(int sectionnumber, context context) { afragment fragment = new afragment(context); bundle args = new bundle(); args.putlong(arg_section_number, sectionnumber); fragment.setarguments(args); return fragment; } public afragment(context context) { mcontext = context; }
how use fragment's getactivity() method pass context ( mcontext = context )?
you should never need pass context fragment.
you can remove newinstance
parameters
wherever use context field in fragment, replace getactivity().
if want use field, must assign in onattach
, remember unassign when fragment stopped or detached activity
Comments
Post a Comment