likejudo
likejudo

Reputation: 3735

How can Main Activity call another Activity A and send its context to it?

My MainActivity calls another Activity A which needs to access some members of MainActivity. What is the best way to send a reference to Main Activity (or its context) to Activity A without resorting to complicated methods like parcelables etc?

There are some heavyweight android wrestling matches here but I am not sure that it is relevant to my problem.

details

I have Alert and Alerted objects in a one-to-many relationship (Alerted represents the various times an Alert was rung).

AlertsListActivity extends ListActivity which displays a list of Alert objects from a SQLite database table (primary key: alertId). It has an AlertsListAdapter.

AlertedsListActivity has a ListFragment which displays a list of Alerted objects from Alerted table (foreign key is alertId from Alert table). It has an AlertedsListAdapter.

AlertsListActivity needs to call AlertedsListActivity to display the list of Alerted objects. I used startActivityForResult().

Inside AlertedsListAdapter

    public View getView(int position, View convertView, ViewGroup parent) {
        final Alert alertItem = (Alert) mainActivity.alertsListAdapter.getItem(position);
        final Alerted alertedItem = (Alerted) getItem(position);
...

I do need the Alert objects also, in order to display some identifying information from them with each Alerted list item. Hence I need the reference to mainActivity.alertsListAdapter

How can AlertedsListActivity access AlertsListActivity?

Update: Since I did not get any solutions, I implemented a workaround. The data that I needed to access from Main Activity, I modified. So the Alert object was made a parcelable, and the SQLOpenHelper was made a singleton.

This allows the data to be accessed from Activity A.

Upvotes: 1

Views: 191

Answers (2)

Fattie
Fattie

Reputation: 12296

Here's the simple, common way to do it:

singletons typically have variables like the below example, "useThisContext" or "mainFeedIsHere".

public class Cloud
    {
    private static Cloud ourInstance = new Cloud();
    private Cloud() { Utils.Log("cloud singleton launched"); }
    public synchronized static Cloud getInstance()
        {
        return ourInstance;
        }

    /////////////////////////////////////////////////

    public Context useThisContext;

another example ...

public class Feed
    {
    private static Feed ourInstance = new Feed();

    private Feed()
        {
        Utils.Log("feed singleton launched");
        freshestPostsForDisplay = new ArrayList<ParseObject>();
        }

    public synchronized static Feed getInstance()
        {
        return ourInstance;
        }

    public List<ParseObject> freshestPosts;
    public MainActivity mainFeedIsHere;

Quite simply when everything launches (or when it changes), those "things" need to set those variables in the singleton. In other words, those things "tell the singleton, where they are." It's that simple.

So, in the MainActivity perhaps, in onCreate, it might say something like...

    CLOUD.useThisContext = this;
    FEED.mainFeedIsHere = this;

Then for example inside Feed.java you may have say

mainFeedIsHere.feedReload();

It goes without saying you have to check that they are not null (but how else could it be?) and you have to keep them up-to-date as it were. (i.e., for whatever reason you may want to change "useThisContext" -- again how else could it be?)

{Sometimes you'll have one "centralised" singleton .. perhaps "State" .. to sort of combine all these together - so that anyone can "get to" any of those "exposed" things as needed. This is, really, how game engines go; so that you can say more or less SoundEffects.Booms() or Tanks.Faster() or AI.FindVillains() at any time anywhere.}

Cheers!

Upvotes: 2

likejudo
likejudo

Reputation: 3735

Since I did not get any solutions, I implemented a workaround. The data that I needed to access from Main Activity, I modified. So the Alert object was made a parcelable, and the SQLOpenHelper was made a singleton.

This allows the data to be accessed from Activity A.

Upvotes: 0

Related Questions