The question is published on by Tutorial Guruji team.
I’m new to fragments and I’m trying to take an activity and convert it to a fragment. A lot of the stuff I’m using needs Context and when I use getActivity() I get a log of warnings that it may return null and I’m not sure how to fix it. I read that I should attach the fragment to the activity somehow, but I’m not sure. I’m basically using one activity and going to have 4 fragments because I want to use a navigation drawer.
This is in the main activity for showing the fragments
@Override public boolean onNavigationItemSelected(@NonNull MenuItem item) { switch (item.getItemId()) { case R.id.nav_calendar: getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new CalendarFragment()).commit(); break; case R.id.nav_survey: getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new SurveyFragment()).commit(); break; case R.id.nav_forum: getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new ForumFragment()).commit(); break; case R.id.nav_logout: logout(); break; case R.id.nav_contact: getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new ContactFragment()).commit(); break; } drawer.closeDrawer(GravityCompat.START); return true; }
And in the fragments onCreateView() I have this, I have a lot of methods and most of them use Context, I added the if statement because of the same warning and that seemed to work there but how should I take care of the rest on Context uses. I don’t see making an if statement for each time a good thing.
View view = inflater.inflate(R.layout.fragment_calendar, container, false); if (getActivity() != null) { ((MainActivity) getActivity()).getSupportActionBar().setTitle(R.string.calendar); } AndroidThreeTen.init(getActivity()); initialBuild(view); buildCalendarView(); setRecyclerView(); return view;
Edit
Related images
Also I tried without the @NonNull
Answer
See https://www.reddit.com/r/androiddev/comments/aklpz4/why_does_getactivity_in_fragment_might_be_null/.
You can use onActivityCreated()
to access getActivity()
, it won’t be null. I usually use onCreate()
or onCreateView()
to get getActivity()
, but in some rare cases a fragment’s view can be created before hosting activity when an application starts (but I don’t remember this in real work).
In any case getActivity()
is marked as Nullable
, so you can compare it with null
in any case.
UPDATE 1
See getActivity() returns null in Fragment function.
Between onAttach()
and onDetach()
you can get getActivity()
, so save context in onAttach()
and later use it in onCreateView()
.
UPDATE 2
Fragment:
private Activity activity; @Override public void onAttach(Context context) { super.onAttach(activity); if (context instanceof Activity){ activity = (Activity) context; } } @Override public void onDetach() { super.onDetach(); activity = null; } @Override public void onCreateView() { // Use activity here. }
UPDATE 3
Sorry,
@Override public void onAttach(@NonNull Context context) { super.onAttach(context); if (context instanceof Activity){ activity = (Activity) context; } }