1

I have a class which encapsulates the information I want to pass over and it is parcelable. So I can pass its properties from one activity to another by using Intent.putExtra() method. But I cannot send images with it since images' sizes are larger than it should be (it doesn't matter whether they are Bitmap or ByteArray).

User downloads images from Firebase Storage and it is very likely that when user views an image, she won't see it again. So saving images in a cache-like storage isn't really necessary.

In Swift, I can do something like this:

// We are in currentViewController which is the activity that will send the image

let someNewViewController = NewViewController() // Create an instance of NewViewController class which is the activity that will receive image

someNewViewController.image = someImage // NewViewController class has a property called image, assign it an image from currentViewController

navigationController.pushViewController(someNewViewController, animated: True) // Go to someNewViewController activity

And then you can easily use image in the someNewViewController by accessing image property. When someNewViewController deallocated, image will be removed by garbage collection.

Is there a similar way in Kotlin/Java? Or is there an alternative to putExtra() that will allow large files to be transferred?

Thanks in advance!

Metin
  • 184
  • 1
  • 12
  • 3
    "Is there a similar way in Kotlin/Java?" -- do not have two activities. Have one activity with two fragments. Use a shared `ViewModel` to allow both fragments to have access to your image. – CommonsWare Jul 23 '19 at 18:24
  • thanks for at least posting code though – a_local_nobody Jul 23 '19 at 18:26
  • Hi @CommonsWare, thanks for your answer. I didn't think that way, I will try to implement it in the way you suggested. – Metin Jul 23 '19 at 18:31
  • Hello again @CommonsWare, I implemented ViewModel as you suggested and it works perfectly, thanks again. What I wanted to ask when transferring data between activities shall we use always ViewModel or for small amounts of data, is sending with Bundle (i.e., Intent.putExtra() ) is preferable? – Metin Jul 24 '19 at 13:00
  • 1
    I tend to use a `Bundle` (e.g., fragment arguments) for small configuration data that is not changing, such as the ID of an object that the fragment is supposed to let the user view or edit. – CommonsWare Jul 24 '19 at 13:13
  • I see, thanks again! – Metin Jul 24 '19 at 13:20
  • can you upload your screen design for explain more your question – yousef Jul 03 '20 at 13:35

0 Answers0