As I am familiar with working with actions and user views, I constantly had to decide where the code should go, which is related to both the view and the parent action. Add to the user objects that should be accessible by both, and the options for how to structure the code are endless. Here are the features of my problem:
Relevant classes / files:
GameActivity extends Activity: The layout used contains some custom views.
MapView extends View: this is contained in the layout used by GameActivity
World: a custom class that defines a custom World object
Expected Result:
GameActivity pulls out a layout using MapView. The onDraw () function of MapView draws a map on the canvas using information from the World object.
Problem:
The World object that MapView requires is loaded from a previously saved file. I can get this object in MapView in different ways, and that is where I get hesitant. I went through the following iterations. (Note that they all work. What I'm looking for is reasons to use one path over another.)
Version1:
GameActivity: all he does is setContentLayout (layout_with_mapview_in_it.xml)
MapView: has all the code to load a World object from a file and uses it to reference the necessary drawing options
World: a simple custom w / 3 parameter class in its constructor
Then I decided that loading the World object from the file should be a method of the World class itself, and not what was done in the onCreate () method of MapView. Since loading a world file is something that you will never get around without a World object, it makes sense that it should be a class method. So I created the loadWorld (String world_name) method in the World class. Now the files looked like this:
Version2:
GameActivity: all he does is setContentLayout (layout_with_mapview_in_it.xml)
MapView: creates a new World object using the constructor, then calls its loadWorld () method to update it with the file information
World: a simple custom class with w / 3 parameters in it constructor and loadWorld () method
Finally, I decided that there should only be drawing in MapView. The whole point of having a World object is to skip it, right? So I moved World Construction / loading from View and to Activity. This led to the need to create the setter method in MapView in order to be able to pass the World object from the parent activity where it is created.
Version3:
GameActivity: sets the layout, creates a World object, and calls its loadWorld () to load it from the file. MapView references by identifier, then calls the setWorld () method of MapView to pass an instance of the World object over
MapView: The World object is set externally. Draws a map using information about this object.
World: a simple custom class with w / 3 parameters in it constructor and loadWorld () method.
Alright, so where am I now. My problem is that, although I like the conventions that I have chosen in terms of the fact that the views contain only code related to the drawing, and storing the methods associated with the classes in their classes - it seems when I switch to this method, I I suddenly create temporary objects more often and transferring objects from activity to activity for viewing and the like. This seems a lot more overhead, but at the same time, what is the whole point of abstraction right? Select the class to then instantiate the object and pass it. But for some reason, it seems to me that the more I abstract things, the more difficult the objects are to process.
I guess I'm trying to ask here, am I making things harder by simply not loading World from a file into MapView myself? Am I just stubborn not wanting to have code that involves reading an object from a file in the View class? Is it better to have it in your class or in the Office? What do I need to consider when making these decisions? Is there a solution to my dilemma that I donโt even suspect?
I think that the answer will be a personal preference, but I want to get an idea of โโwhether there are conventions there to do this anyway, or if there are good reasons why to structure a certain path. I tried to find the answers to this question, but I think that I am using the wrong search terms. I continue to work on material that describes how to structure the structure of the activity / presentation, but contains nothing code inside. When there is code, inevitably someone teaches how to transfer data between actions or between activity and presentation, etc. I know all these methods. I just don't know which one to use.
Some of the links I looked at are:
Multiple Views Android App - Best Practices?
Android - Actions against views
Android Actions and Views
Android: which is better - a few actions or switching views manually?
EDIT: more detailed information on application structure and code samples is included
GameActivity:
public class GameActivity extends Activity implements OnTouchListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.game); Log.d("LOGCAT", "GameActivity Started");
MapView:
public class MapView extends View implements OnClickListener { protected Context context; public World world; public Map<Integer,Bitmap> tileTranslator;
World:
public class World implements Serializable { public String world_name; public int world_width; public int world_height; public int[][] world_map; public World() {
Some codes have been removed to save space. Let me know if any of the deleted codes or other activities will be helpful.
More on what's going on behind the scenes:
The application starts with a MenuActivity, in which there are two buttons, each of which leads to a different action: WorldGenerationActivity or WorldSelectionActivity.
In WorldGenerationActivity, the user is shown a TextEdit screen and a button. They enter parameters for the world they want to generate (world_name, world_width, world_height). As soon as they click the button, a World object is created and saved in the file, using the name world_name as the file name. The file is saved using the saveWorld (String world_name) method, available in the World class. An instance of the newly created World object has a saveWorld () method called, the file is saved, and the user returns to the parent MenuActivity element through a call to complete ().
In WorldSelectionActivity, the user is shown a ListView that is connected to an ArrayAdapter. An array of file names is created from the files contained in the world save folder, and the adapter allows listview to display these file names in a list. The user selects one, and the selection is sent back to parentMenuActivity as a string through aim.putExtra (). WorldSelectionActivity is started for the result, so it ends, and we return to MenuActivity.
When MenuActivity returns a result from WorldSelectionActivity, it stores the putExtra () message in the parameter, and then calls GameActivity. It sends a message to GameActivity through another putExtra ().
GameActivity receives the message and saves it in the world_name variable. Then it creates a World object and passes the world_name string to the loadWorld (String world_name) method of the World class, which loads the specific world requested by the user earlier from the previous file save. I seemed to be silent about how this is handled in my previous explanation. Since I need a World object to load the world, I have to first create a dummy World object in GameActivity, and then call its loadWorld method and pass the result to another newly created World object. This led to the fact that I had to include a parameterless constructor in the World Class, which I did not need there. I am not sure why I could not get it to work without creating a mannequin in the world. I tried to put the logic of reading the file into the constructor without parameters, but that didn't work either. I think I missed something, but this is not my biggest problem at this point.
MapView is one of the views contained in GameView. Entering your onDraw () method requires information from a World object. Previously, I had all the loading and construction in the world, and then I only needed to create the world once and do everything that I wanted with it. As soon as I moved the loadWorld () method from MapView to the World class itself, and also transferred the call to this method from MapView and GameActivity, it seemed like I was suddenly creating temporary World objects everywhere. I am wondering if there is a cleaner way to do this and still keep things in classes that make sense.