如果我更改了第二个活动的图像,并且当我返回到上一个活动时,图像应该更改,我想更改上一个活动的图像。
https://i.stack.imgur.com/7bTnj.png
尝试在代码中自定义以下代码。它会起作用。
这是您要跟踪图像是否已更改并希望加载更改的图像的第一个活动。
public class A extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_a);
//YOUR OTHER CODE HERE..
startActivityForResult(new Intent(A.this, B.class), 111);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 111 && resultCode == RESULT_OK) {
if (data.getExtras() != null) {
//set your image path which has been returned by previous activity to your imageview here
//data.getExtras().getString("image_path")
}
}
super.onActivityResult(requestCode, resultCode, data);
}
}
这是您更改图像的下一个活动
public class B extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_b);
//YOUR OTHER CODE HERE..
}
@Override
public void onBackPressed() {
Intent intent = new Intent();
intent.putExtra("image_path", "CHANGED_IMAGE_PATH");
setResult(RESULT_OK, intent);
super.onBackPressed();
}
}