0

I am trying to create a recycler view with data from another activity. I need to transfer data from one activity to the recycler view activity without it opening the recycler view activity. Because only when I press another button do I want to go to the next activity.

I have tried using intent, however startActivity(intent) always opens the other activity. I also tried using Share preferences to save the data and retrieve it in the other activity. However, Base64.DEFAULT was not working for me.

public void sendData(Bitmap images, String image_class) {
    Intent send = new Intent(getApplicationContext(), Scanned_List_Activity.class);
    send.putExtra("image_class",image_class);
    send.putExtra("image", images);
    startActivity(send);
}
Enowneb
  • 943
  • 1
  • 2
  • 11
Codifer
  • 15
  • 2
  • I did not understand what you were achieving can you explain in detail. – Hanif Shaikh Feb 02 '23 at 04:54
  • You can not [have two activities alive](https://stackoverflow.com/questions/11102337/can-you-have-two-activities-running-at-the-same-time) at the same time. You can replace that with fragments – Zain Feb 02 '23 at 06:38
  • You can use Broadcast receiver to send data to another activity without calling it [here](https://stackoverflow.com/questions/9137477/passing-data-from-broadcast-receiver-to-another-activity) – Rakesh Saini Feb 02 '23 at 07:04

1 Answers1

0

If I understand your question right, you should do it differently.

Activity represents a View or better to say a Controller in the MVC architecture. Data you gained in the RecyclerReceiver comes from a model, activity just passes it to a view. That's why the right way is to have this data in the model layer rather than pass it over an Intent as a Parcelable object.

Nowadays we have a ViewModel class which is a layer between a model and your view. If you use DI tool like dagger you can keep a single instance of your ViewModel between this two activities - when you prepare your dataset on the first activity, the second activity will get it next time when it would be opened.

In short you should put this dataset either in the separate table and request from 2nd activity or to put on the ViewModel which single instance is shared between this two activities.

enter image description here

Gleichmut
  • 5,953
  • 5
  • 24
  • 32