Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
How can i presist data across all my activities so when I change a variable on activity A, that variable will be changed on all activities to which it belongs.
So to play safe and easier I would like to know the right way to acomplish this.
I read some ppl sayng to use static variable but I have like 45-70 variables and 21 classes and doenst feels right to me to just put everything static...
I have also been using intents to pass the data between activities but I have to update it manually all the time wich can lead to inconsistencies sometimes.
My goal is to have a class that can be shared between all activities automatically and replicate any changes across all activities.
What you're looking for here is a model object. Android has a ViewModel object that would work well for this. The ViewModel object should be able to outlive the lifecycle of any activities or fragments that use it. I would recommend checking out that link as it will likely explain how to use a ViewModel more thoroughly than I ever could.
Edit: I misunderstood some parts of your question. Using a static class like you mentioned is definitely an option (and in my opinion, not an anti-pattern). If you would like to persist that data across app restarts, you could also utilize Room.
On a side note, I would also recommend using LiveData inside your model. LiveData is a wrapper of sorts that makes practically any object observable. You'll be able to observe your data in your model object and notify activities that need to know accordingly.
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
I'm newbie programming in java.
I'm doing some forms and in one of them I put some fields, a button and a JTextArea. The idea is when I click in button makes a connection to external database and in JTextArea shows me the connection return (if is okay or if is failed and the error).
So I've created a class with the UI and a class(connection name) with the attributes database, port, username, password ... that make connection to external database.
I don't know if these organization mode is the best way to make it. I should include class connection in the same class of the UI,? How should I pass values of the form to the class connection, by a method?
Could you suggest me how to make it?
As lealceldeiro commented, this is opinion based, but there are some structures that it is good for these types of applications. What I've been taught and have been using is MVC(Model, View, Controller) structure.
The model is responsible for managing the data of the application. It receives user input from the controller.
The view means presentation of the model in a particular format.
The controller responds to the user input and performs interactions on the data model objects. The controller receives the input, optionally validates it and then passes the input to the model.
I usually make packages/modules called controllers, views and models. So I think you're on the right track here.
Let's say you have one class for your GUI, where the button onClickListener is. That would be your View.
Your models would be the class that you use to connect to the database, and maybe some classes/models in which you store the data from your database queries.
For you I would suggest to make another class that will be your controller. This class would contain your database object, and you can use this to make queries to the database and update your models. One of these could be connectToDatabase(), and you could call this method from your GUI when the button is clicked.
I hope I explained this well enough, and good luck!
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I'm new to MVP, Retrofit and Dagger, so I've decided to make a simple app based on Riot Games API, that just shows some game's info in a list. Everything works fine, but it made me wonder: "Is presenter a good place to do such things, like Retrofit operations? I couldn't find anything about it. There are some simple examples how to use Retrofit, but they are sometimes shown even inside an activity class, which doesn't seem right to me.
So, here is my question: Where should I perform such operations to make the code as clean as possible?
My app repository: https://github.com/Mallorax/Rito_Api_Test
If the Model is what Retrofit returns, you can't put the call there.
The View is intended to separated from the Presenter, and subscribes via Callback interfaces, which Retrofit provides out of the box.
Therefore, your only option without involving some Service layer is to put Retrofit in the Presenter.
I suggest you to place some network call to plain class e.g. Interactors for example LoginInteractor which contains loginMethod, than when it finishes pass the result to the presenter class, which will handle the data and will convert (or not) it for some kind of model which is ready to be presented on UI.
Activity/fragment contains Presenter and Presenter contains Interactor(s)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Today I had an interview for test automation in one of the MNC.
They asked me "why do we need to create an object?"
I explained about OOPs concepts with example of individual bank account holders. But he is not convinced. He just need a definition.
What could be a suitable answer for that question?
You require an object to represent state.
At the most simple definition, a class defines behaviour and an instance of a class (an object) represents state.
Of course there are other things like static contexts which can also maintain state, which you can mention also, but above is the clearest answer which I believe they were looking for.
It also always helps to give an example. You could talk about, for example, an Employee class. You would need an object to represent John and another to represent Jane.
I think that this question is kind of generic and does not give much value to an interview. But some generic question should have a generic answer, and here is mine:
We need to create objects in java so we can get instances that have a state inside our application. This allows us to have persistent encapsulated elements that contain any required information, and methods that operate with it.
Just plain basic OOP theory.
There are many reasons why we create a object apart from basic oops
1) To bring up persistent state data to transactional state to perform action (curd and other) and persist back to data storage.(EJB, POJO,etc )
2) Creating handler to serve service and send fluid data across wire like web-service.
3)Stuctural behavior in action.for example you designed a class for a workflow and to make in action state we create a object and serve the behavior example validation , authorization , etc class
All in all to make design time architecture to response based live system
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
This question is relatively easy to answer but i don't really know the answer as i have no real experience with java or libgdx.
Question: I would like to know the most effiecent way to orginize code in java, i have a strong background in php and in my php projects i would use several different php files (think of it as a class) and just reference it in my main page using either "include 'filename'; or require 'filename'; " . I would keep my header.php, content.php and footer.php completely separate and just reference it. This was a very nice method for me as it really kept my code organized.
What i want to know is if there is an easy way to do this in java/libgdx? i am currently making an android application and as you may know there are life cycles such as create(); render(); resize(); etc.
What i had in mind was to put each cycle e.g. create(); into a class of its own and use extends in my main class. Will this even work?
Thanks for any help in advance guys, its really bugging me to have so much of my code in one main class.
The question is very abstract, and you can read lots of books or articles about OOP, design patterns and code styling, so I will just give few practical tips:
You can use MVC pattern. Create three packages yourpackage.view, yourpackage.model, yourpackage.controller and split your classes by this model.
You can create package yourpackage.utiles and put there all classes with only static methods, which are used in all your app.
Don't put too much code into one class, Java is very objected oriented. Very simple example: you have game with 4 screens. You should create parent Screen class, 4 classes which will be extended from Screen for every screen and class ScreenManager which will change screens.
In game dev Singleton is very useful pattern. You can use it for GraphicMangaer, charcter progress holding, dpad controller, etc.
About your question, there is no reason to put create() method into your classes, constructor is enough. What you can do is to create draw() method in every class which draws something and put there all logic of drawing. Then you can just call draw() methods of your classes in your screen render() method.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I have some trouble finding appropriate patterns for what I want to do.
I have a block game with two game modes. In one mode, any removed blocks are replaced by new blocks dropped from the top of the screen. In the other mode, removed blocks are replaced by a complete new row of blocks rising from the bottom and pushing the whole field up. So I thought it would be best to use the Strategy pattern to implement this.
Now the problem is, that this Strategy would need modifying access to all the positions of existing blocks in the playing field, but obviously that information lies within the Strategy's user class (the Game class) and I can't have circular dependencies.
I could pass the whole field as a reference, but I have the additional problem that all the Block generation code lies within the Game class (as it should in my opinion). So the only thing that comes to mind is pass all these methods as function references, but to me that seems like overkill.
So any way to resolve this? Maybe I'm on the wrong track by wanting to use the Strategy Pattern. Help is greatly appreciated.
Bonus points for any hints regarding the use of two Strategy Pattern objects in a class that need access to the same methods which depend on members in the user class.
I'm coding in Java if that is of help
I guess in the end it was kind of too complex to assume an out-of-the-box answer.
I kind of followed Traxdata's hint of decoupling more together with Mister Smith's suggestion to actually pass references to other objects.
My final solution consists of having a FieldManager, a Field and a Strategy class. The FieldManager contains a Field and Strategy instance. The Field class contains the block creation code.
So whenever blocks need to be created or removed, the appropriate function calls the corresponding Strategy's function and passes a reference to the field. So whenever a Strategy needs to create or remove something in the field it can do so via the field object.