Reputation: 3509
I have an Android application in which there are two activities. The start-up activity is where the user chooses category, and the second activity is where the user plays a game and gets a result. This result is then passed back to the first activity to be posted on facebook.
To pass data between activities I use this code:
Bundle extras = new Bundle();
extras.putInt("categoryid", categoryid);
Intent i = new Intent(MenuView.this, CreateTestView.class);
i.putExtras(extras);
startActivity(i);
This goes both ways. Now to my problem: The first time I start the MenuActivity there is no bundle being passed, and therefore I get a nullpointer exception when I try to retreive the extras. How can I use a check at start-up to see if there is a bundle beeing passed or not?
I tried it this way:
Bundle b = this.getIntent().getExtras();
if(b==null){}
else{
noqs = b.getInt("noqs");
point = b.getInt("point");
But this goes as b==null every time, even after finished game and the bundle is sent from the GameActivity.
Upvotes: 0
Views: 3819
Reputation: 8685
From your MainActivity
you could start sub-GameActivity
via startActivityForResult
and once it is finished, then you could receive your game results back via onActivityResult
.
Something like this:
MainActivity:
private void startGameActivity() {
Intent i = new Intent(getApplicationContext(), GameActivity.class);
i.putExtra("some.key.here", "value");
startActivityForResult(i, 0);
}
@Override protected void onActivityResult( int req, int resp, Intent data ) {
super.onActivityResult(req, resp, data);
// process your received "data" from GameActivity ...
}
GameActivity:
public void onCreate( Bundle savedInstanceState ) {
// ...
Bundle b = getIntent().getExtras();
// ... process your extras from MainActivity
}
public void finishMySubActivity() {
Intent data = new Intent();
data.putExtra("some.other.key", GameResultsHere);
setResult(RESULT_OK, data);
finish();
}
Upvotes: 2