gpt4 book ai didi

java - onActivityResult 方法没有被调用

转载 作者:行者123 更新时间:2023-11-30 10:01:53 29 4
gpt4 key购买 nike

第一个 Activity 我想去第二个,然后从第二个第三个​​。在第三个​​activity中,我想在EditText中输入姓名,然后按下按钮后,转到第一个activity,同时发送在第三项 Activity

我遇到过两种情况,第一种情况:finish()方法以调用super.finish()结束。不幸的是,在这种情况下,我没有返回到第一个 Activity ,而是返回到第二个 Activity 。

第二种情况:finish()方法以startActivity(i)调用结束。不幸的是,在这种情况下,MainActivity 中的方法 - onActivityResult 没有被调用,这将导致第三个 Activity 中输入的数据缺失。

我通常如何从我的第三个 Activity 中接收数据?

这是我的代码:

首先:

public class MainActivity extends AppCompatActivity {

TextView textViewInformation;
Button button_GoToSecond;

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

textViewInformation = findViewById(R.id.textView);
button_GoToSecond = findViewById(R.id.button);

button_GoToSecond.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

Intent i = new Intent(MainActivity.this, Second.class);
startActivity(i);
}
});
}

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

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

String name = i.getStringExtra("name");
textViewInformation.setText(name);
}
}
}

第二个:

public class Second extends AppCompatActivity {

Button button_GoToThird;

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

button_GoToThird = findViewById(R.id.button2);

button_GoToThird.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

Intent i = new Intent(Second.this, Third.class);
startActivity(i);
}
});
}
}

第三(第一版):

public class Third extends AppCompatActivity {

EditText editText_Data;
Button button_SendData;

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

editText_Data = findViewById(R.id.editText);
button_SendData = findViewById(R.id.button3);

button_SendData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

finish();
}
});

}

public void finish() {

String name;
name = editText_Data.getText().toString();

Intent i = new Intent(Third.this, MainActivity.class);
i.putExtra("name", name);

setResult(RESULT_OK, i);
super.finish();
}
}

第三个(第二个版本):

public class Third extends AppCompatActivity {

EditText editText_Data;
Button button_SendData;

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

editText_Data = findViewById(R.id.editText);
button_SendData = findViewById(R.id.button3);

button_SendData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

finish();
}
});

}

public void finish() {

String name;
name = editText_Data.getText().toString();

Intent i = new Intent(Third.this, MainActivity.class);
i.putExtra("name", name);

setResult(RESULT_OK, i);
startActivity(i);
}

最佳答案

将您的 finish 方法更改为:

public void finish() {

String name;
name = editText_Data.getText().toString();

Intent i = new Intent(Third.this, MainActivity.class);
Bundle bundle = new Bundle();
bundle.putString("name", name);

startActivityForResult(i, RESULT_OK, bundle);
}

关于java - onActivityResult 方法没有被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57226911/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com