Reputation: 1858
I am trying to pass a drawable between activities. I am unsure as to whether I can just get a drawables resourceID from an alertdialog and pass that value as an Int to another activity.
I have explored changing the drawable to a bitmap, and I have also tried getResources().getIdentifier() but perhaps I was using them incorrectly.
What should I do to pass a drawable between activities using .putExtras(bundle)
public class CreateActivity extends Activity implements OnClickListener {
EditText etTitle;
Button btDate;
Button btTime;
Button btPic;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
//onclicklistener
findViewById(R.id.btn_confirm).setOnClickListener(this);
findViewById(R.id.btn_back).setOnClickListener(this);
etTitle = (EditText) findViewById(R.id.editTextTitle);
btDate = (Button) findViewById(R.id.btn_date);
btTime = (Button) findViewById(R.id.btn_time);
btPic = (Button) findViewById(R.id.btn_picture);
}
// Will be called via the onClick attribute
// of the buttons in main.xml
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_confirm:
String title = etTitle.getText().toString();
String time = btTime.getText().toString();
String date = btDate.getText().toString();
Drawable drawable = getResources().getDrawable(R.id.btn_picture);
Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();
Log.e("LOG", title);
Log.e("LOG", time);
Log.e("LOG", date);
Bundle newBundle = new Bundle();
newBundle.putString("TITLE", title);
newBundle.putString("TIME", time);
newBundle.putString("DATE", date);
//Trying to pass a drawable from one activity to another
newBundle.putParcelable("BITMAP", bitmap);
Intent intent = new Intent(this, MainActivity.class);
intent.putExtras(newBundle);
setResult(RESULT_OK, intent);
finish();
break;
case R.id.btn_back:
finish();
break;
}
}
public void showTimePickerDialog(View v) {
DialogFragment newFragment = new TimePickerFragment();
newFragment.show(getFragmentManager(), "timePicker");
}
public void showDatePickerDialog(View v) {
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getFragmentManager(), "datePicker");
}
public void showPicturePickerDialog(View v) {
DialogFragment newFragment = new PicturePickerFragment();
newFragment.show(getFragmentManager(), "picturePicker");
}
}
Upvotes: 1
Views: 4109
Reputation: 8304
I suggest you pass the identifier, which is just an integer. Rebuild the drawable as needed in the next activity.
intent.putIntExtra(DRAWABLE_KEY, R.id.your_drawable_id);
Upvotes: 2
Reputation: 93708
As long as its staying inside the same apk, I'd just transfer the id of the drawable (R.id.btn_picture) as an int, since all services/activities can grab resources.
Upvotes: 0