[英]Scope problem - put intent extra and start activity from 2 different functions
I have 2 listeners, 1 needs to get the intent extra, which is an image uri, and the other one needs to start the other activity with that extra. 我有2位听众,其中1位需要获得额外的意图,这是一个图像uri,另一位需要从该目标开始其他活动。 But I think that due to them being in different scopes, the extra is not really put for the intent that starts the other activity. 但是我认为,由于它们处于不同的范围,因此并未真正为启动其他活动的意图提供额外的费用。
This is the code: 这是代码:
private Intent intent = new Intent(this, OtherActivity.class);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button button= findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
intent = new Intent(v.getContext(), OtherActivity.class);
startActivity(intent);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == Thing.SPECIFIC_REQUEST_CODE) {
Thing.ActivityResult result = Thing.getActivityResult(data);
if (resultCode == RESULT_OK) {
Uri resultUri = result.getUri();
intent.putExtra("imageUri", resultUri.toString());
}
}
}
I want that the extra that I put in onActivityResult
will be sent to the activity started by the onClickListener
when the button is clicked 我希望我在onActivityResult
放入的额外内容将被发送到单击按钮时onClickListener
启动的活动
my suggestion is to use a field to store the image URI : 我的建议是使用一个字段来存储图像URI:
1- define a String variable in your class 1-在您的类中定义一个String变量
String imageURI = "";
2- in your onActivityResult : 2-在您的onActivityResult中:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == Thing.SPECIFIC_REQUEST_CODE) {
Thing.ActivityResult result = Thing.getActivityResult(data);
if (resultCode == RESULT_OK) {
Uri resultUri = result.getUri();
imageURI = resultUri.toString();
}
}
3-in your onClickListener : 3在您的onClickListener中:
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
intent = new Intent(v.getContext(), OtherActivity.class);
intent.putExtra("imageUri", imageURI);
startActivity(intent);
}
});
In the onClick
you are re-initializing the Intent, so any extras you may have set are lost. 在onClick
您正在重新初始化Intent,因此您可能设置的所有附加功能都将丢失。 If you remove the line above startActivity
you should be fine. 如果删除startActivity
上方的行,就可以了。
I would not go that way thought. 我不会那样想。 I would not have the Intent as a class variable but rather the image URI. 我不会将Intent作为类变量,而是将图像URI。 So whenever a click event happens, I would check for the image URI having been set, create a new Intent and then start the Activity. 因此,每当发生单击事件时,我都会检查是否已设置图像URI,创建新的Intent,然后启动Activity。
Your code would become like this: 您的代码将如下所示:
private String intentImage = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (intentImage != null) {
Intent intent = new Intent(this, OtherActivity.class);
intent.putExtra("imageUri", intentImage);
startActivity(intent);
}
});
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == Thing.SPECIFIC_REQUEST_CODE) {
Thing.ActivityResult result = Thing.getActivityResult(data);
if (resultCode == RESULT_OK) {
Uri resultUri = result.getUri();
intentImage = resultUri.toString();
}
}
}
Also keep in mind that you don't have any startActivityForResult
in your code. 还请记住,您的代码中没有任何startActivityForResult
。 So if this is your full activity, you would never get onActivityResult
invoked. 因此,如果这是您的全部活动,则永远不会调用onActivityResult
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.