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

Popular posts from this blog

Retrieving ETA (estimated time of arrival) with Google Distance Matrix API and public transit as transport mode -

android - ConstraintLayout: Realign baseline constraint in case if dependent view visibility was set to GONE -

c# - Populating Gridview inside Listview ItemTemplate On Web User Control from Code Behind -