简体   繁体   中英

My activity does not call onActivityResult

I'm trying to code 2 activities with startActivityForResult. But my activity does not call 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".

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.

It may be a good idea ensure the value you're using for reqCode (WANTGPS) is positive.

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); OTOH small integers (1,2,100,...) seem to work fine. ReqCode might have an upper limit as well. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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