简体   繁体   English

如何检查是否有意图发送的捆绑包?

[英]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.

相关问题 如何查看来自我的Android应用程序的已发送电子邮件? 还是打算完成(无论发送了什么电子邮件)? - How can I check sent emails from my Android application? Or intent upon completion (regardless of email sent)? 无法接收从Intent发送的Bundle中找到的任何值 - Can't receive any value found in Bundle sent from an Intent 如何检查是否创建了Intent - How can i check if a Intent was created 如何对从Activity中启动/发送的Intent进行单元测试? - How can I unit test an Intent launched/sent from an Activity? 如何检查是否可以通过手机发送电子邮件? - How can I check if emails can be sent from my phone? Android-如何检查“ stopService”意图的发送者类? - Android - How can I check the sender class of the “stopService” Intent? 如何检查活动中收到的未决意图? - how can I check the pending intent is received in an activity? 如何检查一个活动是否收到另一个活动发送的意图? - How to check if an Activity receives an intent sent from another activity? 如何检查发送的预期意图而不是在Espresso中实际启动活动? - How to check expected intent sent without actually launching activity in Espresso? 如何发送Android意向以将当前笔记保存在evernote中,以便可以使用其他意向创建新笔记? - How do I sent an Android intent to save the current note in evernote so that a new one can created with another intent?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM