[英]How can I check if there is a bundle sent with the intent?
I have an Android application in which there are two activities. 我有一个Android应用程序,其中有两个活动。 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.
然后将此结果传递回第一个要在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.
现在我的问题:我第一次启动MenuActivity时没有传递包,因此当我尝试检索附加内容时,我得到一个nullpointer异常。 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. 但是每次都是b == null,即使在完成游戏并且从GameActivity发送包之后也是如此。
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
. 从您的
MainActivity
您可以通过startActivityForResult
启动子GameActivity
,一旦完成,您就可以通过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: 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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.