简体   繁体   中英

Give Result to the same Activity

I have a Activity i use to get a folderpath. To Navigate to the next folder i create a intent to this Activity with the path of the folder. When i try to return the result it gets lost in the middle.

Giving the result:

@Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        Intent returnIntent = getIntent();
        switch (item.getItemId())
        {
            case R.id.action_ok:
                returnIntent.putExtra("path", filename);
                Log.d("Result", "Picker"+String.valueOf(RESULT_OK));
                setResult(Activity.RESULT_OK, returnIntent);
                finish();
                return true;
            case R.id.action_abort:
                setResult(RESULT_CANCELED, returnIntent);
                finish();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

Navigation (Yes, it's a ListActivity):

@Override
    protected void onListItemClick(ListView l, View v, int position, long id)
    {
        filename = (String) getListAdapter().getItem(position);
        if (path.endsWith(File.separator))
        {
            filename = path + filename;
        }
        else
        {
            filename = path + File.separator + filename;
        }
        if (new File(filename).isDirectory())
        {
            Intent intent = new Intent(this, DirectoryPicker.class);
            intent.putExtra("path", filename);
            startActivityForResult(intent,0);
        } 
        this.finish();
    }

Getting the result:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        if (getIntent() != null)
        {
            Intent returnIntent = getIntent();
            setResult(RESULT_OK, returnIntent);
            finish();
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

When i try to return the result it gets lost in the middle.

It is not lost in the middle. You are looking for the result in the wrong place. getIntent() returns the intent through with your Activity was started. It has not knowledge of the result you set in the other Activity . You have to use the third parameter of onActivityResult , Intent data .

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
   if (data != null) {
       setResult(RESULT_OK, data);
       finish();
   }

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