onActivityResult方法调用onbutton单击

时间:2019-02-20 07:17:18

标签: android

enter image description here请写下答案 如何在按钮单击时调用片段的onActivityResult

enter image description here

Future exception was never retrieved
future: <Future finished exception=ConnectionClosed('WebSocket connection is closed: code = 1006 (connection closed abnormally [internal]), no reason',)>
Traceback (most recent call last):
  File "/abc/lib/python3.6/site-packages/websockets/protocol.py", line 674, in transfer_data
    message = yield from self.read_message()
  File "/abc/lib/python3.6/site-packages/websockets/protocol.py", line 742, in read_message
    frame = yield from self.read_data_frame(max_size=self.max_size)
  File "/abc/lib/python3.6/site-packages/websockets/protocol.py", line 815, in read_data_frame
    frame = yield from self.read_frame(max_size)
  File "/abc/lib/python3.6/site-packages/websockets/protocol.py", line 884, in read_frame
    extensions=self.extensions,
  File "/abc/lib/python3.6/site-packages/websockets/framing.py", line 99, in read
    data = yield from reader(2)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/asyncio/streams.py", line 666, in readexactly
    raise IncompleteReadError(incomplete, n)
asyncio.streams.IncompleteReadError: 0 bytes read on a total of 2 expected bytes

The above exception was the direct cause of the following exception:

websockets.exceptions.ConnectionClosed: WebSocket connection is closed: code = 1006 (connection closed abnormally [internal]), no reason

1 个答案:

答案 0 :(得分:0)

定义常数

public static final int REQUEST_CODE = 1;

使用意图

Intent intent = new Intent(Activity.this,
                    XYZActivity.class);
            startActivityForResult(intent , REQUEST_CODE);

现在使用onActivityResult检索结果

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        try {
            super.onActivityResult(requestCode, resultCode, data);

            if (requestCode == REQUEST_CODE  && resultCode  == RESULT_OK) {

                String requiredValue = data.getStringExtra("key");
            }
        } catch (Exception ex) {
            Toast.makeText(Activity.this, ex.toString(),
                    Toast.LENGTH_SHORT).show();
        }

    }

在下一个屏幕中,使用此代码设置结果

Intent intent = getIntent();
intent.putExtra("key", value);
setResult(RESULT_OK, intent);
finish();