简体   繁体   English

我的活动未调用onActivityResult

[英]My activity does not call onActivityResult

I'm trying to code 2 activities with startActivityForResult. 我正在尝试使用startActivityForResult对2个活动进行编码。 But my activity does not call onActivityResult. 但是我的活动没有调用onActivityResult。

EDIT : I open an dialogBox. 编辑:我打开一个对话框。 When i click on Cancel, the box must not show up again. 当我单击“取消”时,此框一定不能再次显示。 When i click on remind me later, if i go to the second acitivty and go back to home, the box must show up. 当我单击“稍后提醒我”时,如果我第二次参加活动并返回家中,则必须显示该框。

I don't understand. 我不明白 I have check on stack overflow but no right anwser. 我检查了堆栈溢出,但没有正确的答案。

Can you help me please =) 你能帮我吗=)

Thx 谢谢

First activity : 第一次活动:

...
public void onCreate(Bundle saveInstance) {
...
Intent i = new Intent(HomeActivity.this, EstablishmentActivity.class);

                int idEstablishment = listEstablishment.get((int)id).getIdEstablishment();
                Log.d(TAG, "id = "+idEstablishment);
                i.putExtra("idEstablishment", idEstablishment);
                i.putExtra("GPS", wantGPS);
                Log.d(TAG, "wantGPS : " + wantGPS);
                //Start Intent
                startActivityForResult(i, WANTGPS);
...
}

...

 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {

        Log.d(TAG, "go on actovoty rsult");


        if (requestCode == WANTGPS) {
            if (resultCode == Activity.RESULT_OK) {
                wantGPS = intent.getExtras().getBoolean("GPS");
                Log.d(TAG, "result : "+wantGPS);
            }
        }
}

And 2nd activity : 第二活动:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.establishment_main);

        // Set up the action bar.
        final ActionBar actionBar = getActionBar();
        if (actionBar == null) {
            Log.d(TAG, "actionBar is null !");
        }

        //Set up the left icon to go back to Home Page
        actionBar.setDisplayHomeAsUpEnabled(true);

        Intent i = getIntent();
        wantGPS = i.getBooleanExtra("GPS", false);
        Log.d(TAG, "wantGPS 2 "+wantGPS);


    }

...

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
     if (id == android.R.id.home) {
            Intent result = new Intent(EstablishmentActivity.this, HomeActivity.class);
            result.putExtra("GPS", wantGPS);
            setResult(RESULT_OK, result);
            finish();
        }
        return super.onOptionsItemSelected(item);
    }

I have the answer !! 我有答案! If you have the same problem than me, this answer should help you :) 如果您遇到的问题与我相同,则此答案应为您提供帮助:)

I had define parent activity on the manifest. 我在清单上定义了父级活动。 So android does not look into "OnOptionItemSelected". 因此,Android不会查看“ OnOptionItemSelected”。

I removed parent activity on manifest and it works ! 我删除了清单上的父级活动,该方法有效!

According to answers on this post: 根据这篇文章的答案:

Try the following code instead: 请尝试以下代码:

    Intent result = new Intent();
    result.putExtra("GPS", wantGPS);
    setResult(RESULT_OK, result);
    finish();

Using the simpler Intent() constructor seems to solve a lot of problems. 使用更简单的Intent()构造函数似乎可以解决许多问题。

It may be a good idea ensure the value you're using for reqCode (WANTGPS) is positive. 确保您为reqCode(WANTGPS)使用的值是正数是一个好主意。

I had previously run into problems with (randomly-generated) negative reqCodes, and onActivityResult didn't get fired about half of the time(when reqCode goes negative); 我以前曾遇到过(随机生成的)负reqCodes的问题,并且onActivityResult在大约一半的时间没有被解雇(当reqCode变为负数时); OTOH small integers (1,2,100,...) seem to work fine. OTOH小整数(1,2,100,...)似乎可以正常工作。 ReqCode might have an upper limit as well. ReqCode也可能有上限。 Unfortunately I could not find any mention to this kind of behavior in the docs though; 不幸的是,尽管如此,我在文档中找不到任何提及这种行为的方法。 the Android documentation did not seem to explain what reqCode should be. Android文档似乎并未解释reqCode应该是什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM