I have two activities in android studio. In Activity1 I have a button or image view and I want to when I clicked on it, send an image to Activity2 and when I get to Activity2, the image will be there. And when in Activity1 I clicked the button for second time, the Image in Activity2 will be gone. but I dont want click on image view and go to second activity. I want to click on it in first activity and just send the image to sec activity without go to sec activity automatically. and when I went to sec activity manually, the image will be there. And when I return to first activity and click the image view again, the image in sec activity will be disappear . how can I do that?
public class MainActivity extends AppCompatActivity {
byte[] bytesImage;
public static final String MyPREFERENCES = "MyPrefs";
//use shared preferences to save button click count
SharedPreferences sharedPreferences;
String hide_image;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sharedPreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
final ImageView imageview=(ImageView) findViewById(R.id.imageView);
imageview.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (sharedPreferences.getString("activity1", "").equals("1")) {
//image is already clicked once
hide_image="1";
}
else{
hide_image="0";
imageview.buildDrawingCache(); //your imageview
Bitmap bitmap = imageview.getDrawingCache();
ByteArrayOutputStream ba = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 50, ba);
//save the image byte array
bytesImage=ba.toByteArray();
SharedPreferences.Editor editor = sharedPreferences.edit();
//set 1 as button is clicked
editor.putString("activity1", "1");
editor.commit();
}
}
});
Button btn=(Button) findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(MainActivity.this,Main2Activity.class);
i.putExtra("byteArray", bytesImage);
i.putExtra("hideImage", hide_image);
startActivity(i);
}
});
}
}