简体   繁体   中英

Set an ImageView from a different Activity

I have two Activities, in the secondary activity I have some ImageViews, by selecting one I want it to take me back to the main Activity and to show me the ImageView with the image selected in the secondary Activity. How can I do it?

You can send a callback to your second activity. Pass an interface SetImageViewInterface from your first activity to your second activity. On your first activity you should implement it. In the second activity you should call it and pass the imageView. Then you can call finish() to go back to the first activity.

public SetImageViewInterface interface{

   void setImageView(ImageView imageView);
}

First Activity:

   public MyActivity extends Activity implements SetImageViewInterface {

      public void setImageView(ImageView imageView){
         //set the imageView from second activity to first activity
      }
   }

Second Activity:

imageView.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
         //here you pass the image to your first activity
         setImageViewInterface.setImageView(imageView);
    }
});

You can read more about passing interfaces on this answer: Android: How to send interface from one activity to another

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM