如何将变量从 activity 传递到不在 activity 中的上一个对话框
How to pass a variable from an activity to a previous dialog not in the activity
我没见过这样的案例。我有一个 activity 是从对话框上的按钮启动的。我需要从这个 activity 中获取一个变量,关闭它,然后将它传递回对话框。我的方法是什么?
class ColorPickDialog(val activity: Activity, color: Int, val callback: (color: Int) -> Unit) {
lateinit var savedColorsButton: Button
val currentColorHsv = FloatArray(3)
init {
Color.colorToHSV(color, currentColorHsv)
val view = activity.layoutInflater.inflate(R.layout.d_colorpicker, null).apply {
savedColorsButton = findViewById(R.id.saved_colours_button)
savedColorsButton.setOnClickListener{
val intent = Intent(this.activity.applicationContext, DisplayColorsActivity::class.java)
intent.putExtra("SettingState", true)
this.activity.applicationContext.startActivity(intent)
}
}
这是对话框打开的activity。
public class DisplayColorsActivity extends Activity {
public void displayColors() {
ArrayList<ColourRGB> coloursList = colourStorage.getColours();
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
btn = (Button) findViewById(R.id.select_color_btn);
if (getIntent().getExtras() != null && getIntent().getExtras().getBoolean("SettingState")) {
btn.setVisibility(View.VISIBLE);
}
else {
btn.setVisibility(View.INVISIBLE);
Log.v("Status+", "INot there" );
}
}
public void selectButtonClicked(View view){
finish();
}
}
我需要将一个变量从 DisplayColorsActivity 传递回 ColorPick 对话框,可能在 selectButtonClicked 函数中,(使用按钮将您带回对话框)请注意第一个片段是 Kotlin,第二个是Java
声明一个你选择的常量:
static final int COLOR_PICKED_REQUEST = 1234;
在您的对话框中替换
this.activity.applicationContext.startActivity(intent)
和
this.startActivityForResult(intent, COLOR_PICKED_REQUEST);
并添加一个方法:
public void userPickedColor(int color){
Log.d("TAG", "COLOR:"+color);
}
在您的 DisplayColorsActivity
添加:
Intent intentMessage=new Intent();
intentMessage.putExtra("COLOR",valueOfYourColor);
setResult(COLOR_PICKED_REQUEST,intentMessage);
高于 finish();
在持有你的对话框的 activity 中,确保你有对对话框本身的引用,如 mActionDialog
然后覆盖:
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
if (requestCode == COLOR_PICKED_REQUEST) {
// Make sure the request was successful
if (resultCode == RESULT_OK) {
int color = data.getIntExtra("COLOR");
mActionDialog.userPickedColor(color);
}
}
}
我没见过这样的案例。我有一个 activity 是从对话框上的按钮启动的。我需要从这个 activity 中获取一个变量,关闭它,然后将它传递回对话框。我的方法是什么?
class ColorPickDialog(val activity: Activity, color: Int, val callback: (color: Int) -> Unit) {
lateinit var savedColorsButton: Button
val currentColorHsv = FloatArray(3)
init {
Color.colorToHSV(color, currentColorHsv)
val view = activity.layoutInflater.inflate(R.layout.d_colorpicker, null).apply {
savedColorsButton = findViewById(R.id.saved_colours_button)
savedColorsButton.setOnClickListener{
val intent = Intent(this.activity.applicationContext, DisplayColorsActivity::class.java)
intent.putExtra("SettingState", true)
this.activity.applicationContext.startActivity(intent)
}
}
这是对话框打开的activity。
public class DisplayColorsActivity extends Activity {
public void displayColors() {
ArrayList<ColourRGB> coloursList = colourStorage.getColours();
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
btn = (Button) findViewById(R.id.select_color_btn);
if (getIntent().getExtras() != null && getIntent().getExtras().getBoolean("SettingState")) {
btn.setVisibility(View.VISIBLE);
}
else {
btn.setVisibility(View.INVISIBLE);
Log.v("Status+", "INot there" );
}
}
public void selectButtonClicked(View view){
finish();
}
}
我需要将一个变量从 DisplayColorsActivity 传递回 ColorPick 对话框,可能在 selectButtonClicked 函数中,(使用按钮将您带回对话框)请注意第一个片段是 Kotlin,第二个是Java
声明一个你选择的常量:
static final int COLOR_PICKED_REQUEST = 1234;
在您的对话框中替换
this.activity.applicationContext.startActivity(intent)
和
this.startActivityForResult(intent, COLOR_PICKED_REQUEST);
并添加一个方法:
public void userPickedColor(int color){
Log.d("TAG", "COLOR:"+color);
}
在您的 DisplayColorsActivity
添加:
Intent intentMessage=new Intent();
intentMessage.putExtra("COLOR",valueOfYourColor);
setResult(COLOR_PICKED_REQUEST,intentMessage);
高于 finish();
在持有你的对话框的 activity 中,确保你有对对话框本身的引用,如 mActionDialog
然后覆盖:
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
if (requestCode == COLOR_PICKED_REQUEST) {
// Make sure the request was successful
if (resultCode == RESULT_OK) {
int color = data.getIntExtra("COLOR");
mActionDialog.userPickedColor(color);
}
}
}