-1

I want to make a recipes app, the basic layout is the same for all the recipes the only thing that changes are the images, times and ingredients.

The problem is, I could make 40 activities, one for each recipe and performance wouldn't be a problem because the user is only interacting with one activity at the time. However, writing the same code and going on a copy paste spree feels wrong.

I would have to repeat the same code over 40 activities and it would work (I guess), but it would be much easier to create one activity with the functionalities I want like a timer and the layout and in some way make smaller files that insert the data for the selected recipe in that "pre-made template".

There's must be a way of doing it, although I'm not experienced enough

Here is an example layout

Phantômaxx
  • 36,442
  • 21
  • 78
  • 108
newbieCoder.pkg
  • 255
  • 2
  • 14
  • 1
    I would not have a type for each recipe. I'd have a Recipe that could compose collections of images, times, and ingredients. Prefer composition over inheritance. – duffymo Apr 09 '18 at 18:41
  • 1
    What you talking about is achieved by using parameters. Suppose you start your recipe activities using an `Intent`. Then you can put some parameters into intent's Extras and have your activity display recipe based on those parameters. Then you will only have one activity instead of 40. If you want more of a complete answer, you'll have to share some of the code. – M. Prokhorov Apr 09 '18 at 18:45

2 Answers2

0

It is usually good practice to have a base activity that implements all the code common to several activities then those activities can simply inherit from the base activity like this public class ChildActivity extends BaseActivity.

This will allow you to call methods that are in the BaseActivity from any of the child activities. You can have a read up on Java Inheritance here and here is a blog post with some examples of using a base activity.

aliaksei
  • 676
  • 1
  • 9
  • 22
0

You can create only one activity that will receive the Receipt data as extra using Intent. The layout for this activity should contain an image view(or a recycler view to hold all your images), a recyclerview to show your steps/ingredients and a textview for the time. Receiving these data from the activity(that one that the user selected which receipt he wants too check) that created this new activity, all you need to do is to setup your layout with this data.

Check this question to get how to pass data between activities Click here to see how to create recycler views.

Massita
  • 311
  • 2
  • 10