1

I need the MainActivity-object as context in a Fragment-object.

  1. Passing MainActivity as 'this' to a custom constructor of a Fragment-class works only when starting up the app. When rotating it calls the standard, null-argument-constructor via super.onCreate(savedInstanceState);

  2. Creating a new instance of MainActivity in Fragment does not work either. E.g:

    MainActivity ma = new MainActivity(); AdapterTasks at = new AdapterTasks(ma,title, subt, imgid);

-->System services not available to Activities before onCreate()

How can I get a reference from the MainActivity object to a Fragment-object??

Jomme
  • 1,256
  • 4
  • 14
  • 26

3 Answers3

3

If using Java, you can simply call getActivity()

If you're using Kotlin, you can also use requireActivity() which returns a non-null where getActivity() returns nullable, causing you to have lots of null checks in your code.

kjanderson2
  • 1,209
  • 12
  • 23
  • Just wonder how it would behave within `onDetach()` ...I'd prefer `null` over whatever else it may return. Trading null-checks for lots of `?`, `!!`, ...is not much better and the question seems to be Java, else there wouldn't be any `;`. – Martin Zeitler Jan 16 '20 at 20:05
  • If you use requireActivity or requireContext it will throw an exception if null. This SO post has a pretty good explanation of require methods https://stackoverflow.com/questions/49289281/android-support-library-27-1-0-new-methods-requireactivity-requirecontext/49289578 Or you can take a look at some explanation here https://android.jlelse.eu/the-requireactivity-and-requirecontext-example-1c089ce11a3a I know that the question was asked in Java but I thought a Kotlin answer may be useful to somebody else who may have a similar question. I will edit my answer to support both – kjanderson2 Jan 16 '20 at 23:36
2

Within a Fragment, simply call this.getContext().

Or when you need to access something in the parent MainActivity:

MainActivity activity = (MainActivity) this.getActivity();

Both only works while the Fragment is attached to an Activity. Generally, this.getContext() prevents one to add too much code into the Activity, which rather should be added into the Fragment. One can do quite anything in a Fragment, while letting the Activity inflate it.

Martin Zeitler
  • 1
  • 19
  • 155
  • 216
  • What is actually the difference between 'context' and 'activity'? – Jomme Jan 16 '20 at 19:51
  • @Jomme https://stackoverflow.com/questions/6518206/what-is-the-difference-between-activity-and-context ... it's `ContextThemeWrapper` vs. `ContextWrapper`...there's also `getApplicationContext()`, which also has no theme (and it can get tricky, when passing the wrong one `Context` without a theme, when one would require one). It's not documented too well, but it's an important difference. – Martin Zeitler Jan 16 '20 at 19:54
1

You don't need to pass the MainActivity.

On your fragment, use this:

activity = (MainActivity) getActivity();
Lucas Moretto
  • 404
  • 1
  • 6
  • 18