Reputation: 181
Lets say I have a mainactivity controlling the flow of my app. In this activity I have some list of data that gets updated occasionally.
The mainactivity starts other activities based on user inputs.
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (id == 0) {
openLogin();
}
if ( id == 1) {
Intent intent = new Intent(getApplicationContext(), MyGame.class);
intent.putExtra("game", getGame());
intent.putExtra("bonus", getBonusFields());
startActivityForResult(intent, 0);
}
if ( id == 2) {
scoreboardIntent = new Intent(getApplicationContext(), ScoreBoard.class);
Bundle scoreboardBundle = new Bundle();
scoreboardBundle.putSerializable("players", scores);
scoreboardIntent.putExtra("players", scoreboardBundle);
scoreboardOpen = true;
startActivityForResult(scoreboardIntent, 0);
}
}
What I would like to do is instead of starting an Intent with the ScoreBoard.Class, I would like to start it with a reference to a ScoreBoard object so that it's possible to do something like this:
ScoreBoard scoreboard = new ScoreBoard();
startActivityForResult(scoreboardIntent, 0);
//then if my datalistener triggers
scoreboard.updateStuff();
I know I could move the listener to the Scoreboard class, but I was wondering if it is possible to do it like this as well.
Any insight would be appreciated.
EDIT: Following CommonsWare's analogy, the activities would be separate pages. Currently there are three activities, and while they are all separate two of them need access to the same DB. 1. the main menu. This is a listview containing (among other things) options to start a new game or view the leaderboard. 2. Game. This Activity does not interact with anything else except the callback from mainmenus startActivityForResult(); After the game has ended the score is returned and needs to be stored in the DB somewhere. 3. Leaderboard. This activity shows the top scores in a list.
So here's my dilemma: If I place the database listener in mainmenu I will be able to store the Game callback into the DB, and I will be able to trigger the leaderboard with a list passed via bundle or intentextras. I will however not be able to update the leaderboard when my listener reacts to a change in the database. If I place the DB listener in the leaderboard I will be able to show changes in the underlying data immediately but I will not be able to save scores from new games without also adding DB objects in the mainmenu.(Unless I am missing something)
I am thinking the best solution would be to let mainmenu have access to DB and make the leaderboard into a fragment list that when active shows on top of the mainmenu. Any suggestions for other solutions or explanations of what I'm missing would be greatly appreciated, as it seems like my understanding on how activities communicate may be a little flawed.
Upvotes: 0
Views: 63
Reputation: 1007658
but I was wondering if it is possible to do it like this as well
No, for two reasons:
You cannot create a working activity instance yourself via the constructor
There is no Intent
mechanism to pass an Activity
to another Activity
If these activities are that tightly coupled, perhaps they should not be separate activities at all, but rather be one single activity.
To draw an analogy: if this were a Web app, would this functionality belong on two separate Web pages, or one page? If the latter, use one activity. If the former, use two activities... but then you need to deal with the separation between those activities, just as you would need to deal with the separation between the Web pages in the Web app.
Upvotes: 2