Search code examples
androidandroid-fragmentsandroid-activity

Fragments have "getActivity()". What do additional Activities have that mimics this behavior?


In Android development using Java, Fragments have "getActivity()" that will give you the reference to your MainActivity. I am wondering if there is a method that gets the same exact type of reference of MainActivity but from a different Activity, rather than a Fragment.

IE: (Not in code, more of a diagram)

    MainActivity - "this"          : MainActivity reference
    Fragments    - "getActivity()" : MainActivity reference
    Activity 2   - "this"          : Activity 2 reference
    Activity 2   - ????            : MainActivity reference
    Activity 3   - "this"          : Activity 3 reference
    Activity 3   - ????            : MainActivity reference

In this example, I am attempting to visually show you what I am looking for. Essentially, fill in the "????" (Hopefully it makes sense)

I have tried many things and researched plenty. There are plenty of posts describing a few right ways to pass data between activities and many more wrong ways, but this is not about passing data between activities. It is about having a reference to the MainActivity from within other activities.

Is having access to MainActivity from other activities possible? Or is it just bad practice? It seems like it should be as easy as getActivity() is from a Fragment, but maybe I am missing the concept.

Things I have tried that have not worked are. 
1. Passing the MainActivity reference itself through an Intent, this fails because Activities are not Parceable. (I can pass parceable data and other variables just fine, but not an Activity)
2. I have tried making the activity a child of MainActivity in the AndroidManifest and as well as adding meta data claiming it is the child. This is help get a non_null return from get getParentActivityIntent(); However, it does not provide any way to get that MainActivity reference from that.
3. I have tried getParent(); This always returns null.
4. I have tried getCallingActivity(); This always returns null.
5. I have tried getApplicationContext(); This doesn't seem to have a way to funnel down to a MainActivity reference.
6. Definitely many more things that aren't worth mentioning or forgotten.

I am essentially road blocked for 2 days now researching this, until I can figure out a way to get this reference. There are many posts you would think are relevant, but do not actually pass the true reference, or answer my question directly or even indirectly. The thing that always road blocks me in Android Java is not being able to reference something I need to get a reference to. Every. Single. Time. Everything else usually flows pretty smooth when you can access the things you need access. :)

Thank you for your help and time.


Solution

  • That's not how activities work, no. Each activity is totally independent of every other activity and there's no way for you to safely access any other activity. That's why abstractions that live within a single activity like fragments are useful and why the Single Activity pattern is recommended.