MVC Doubt on Android

Asked

Viewed 293 times

2

Would you like to know the following, in Android development using MVC, for each Activity I have to have a Controller and maybe a DAO? Or can I use the same Controller class that controls two Activity? Does it lose a lot of performance or does it weigh a lot to create so many classes? The model I know I can use in other controller and Activity.

I’m also always having to pass my controller to the DAO, because I need to wait for a callback on a thread and then after the finish I ask the DAO to call a method of this Controll that was passed by parameter. For example controller.retornoCadastroUsuario(exception) where in this method he would check the Exception and possibly ask to display something using some view method (which was also passed as parameter in the controller). That’s right? I’ve been reading a lot of MVC - MVP on Android but I get confused when I’m using a database.

2 answers

1

Regarding the DAO, it refers to the model, it has "nothing to do" with your Activity, if you need to access 3 models, that there are 3 different Daos, you will do it independently.

  • What about the Controll question? It is necessary to have one for each Activity?

  • @Hamon, brother, I only answered about the DAO because it is a Pattern that can be used in various projects, and I use it directly in my web projects, not only in mobile projects. I know a little Android, but I don’t know Controll

  • I think the controlls would be for each entity. because the methods can be called wherever needed. For example, in Usuario_controller, you have the login() method that you can call in Activity login, not in Activity of registration

  • If you are a Controller, responsible for passing information from template to view, to Activity, I agree to be one for each model as well.

  • @Adventistaam the entity you would be a Model? But I would have to create in this example two constructs in the controller where one receives Loginactivity and another Loginactivity to trigger visualization methods (which are in Activity) inside the controller. Would it be a problem? That’s why you doubt creating a controller for Activity.

  • Yes. The Model. Usually I use the controller without constructor

  • If you do not use constructor then you pass Activity as parameter in the controller method?

  • Actually I use the controller to communicate with DAO. Type I only send the same values. What would be your idea to pass the Activity?

  • Pass the Activity to the Controller to call some show methodMensage or irParaNovaActivity (which is in Activity) which are things that will involve the display layer "cannot" be executed directly by the Controller. For example, controller does the checks and now needs to open a dialog, so this method to open the dialog would be in the view.

Show 4 more comments

1


Yes, you can use the same controller or dao for as many activities as you need without "weighing it down" (depending of course on how it is done) as in any other platform.

As for the callback in your Activity you can implement an interface of your controller. Something like:

public class MyController {

    public MyController(OnControllerListener onControllerListener){
        this.onControllerListener = onControllerListener;
    }

    public interface OnControllerListener {
        void onControllerCallback(String someString);
    }

    private OnControllerListener onControllerListener;

}

When the event that fires the callback occurs onControllerListener.onControllerCallback(String someString); //Ou qualquer outra coisa que queira na activity

And in Activity when instantiating the controller:

MyController myController = new MyController(new MyController.OnControllerListener() {
    @Override
    public void onControllerCallback(String someString) {

    }
});
  • And as I had commented on the other answer, is it right to pass an Activity (view) to a Controller? So that whenever necessary some modification, alert, dialog, etc (view things) the controller can call, depending on what the controller decides.

  • I wouldn’t say it’s wrong, you know? But the default would be what I tried to demonstrate with the interface.

  • And if I wanted to use for example a Loginactivity and a Cadastroactivity, with different methods, but using the same Controller. That way I would have to create 2 interfaces in the Controller?

  • Yes. Other options would be to have a Bundle as an argument of onControllerCallback(Bundle Bundle) with its object or to pass a json and deserialize in the activities. Your thread gets what?

  • Friend, I apologize, I will not be able to answer you because I am layman on the subject dev Android. One other thing, I need you to answer me one question. By registering a user then it would be (view -> controller -> model -> DAO (from here it makes the reverse path leading the result)). And to do this reverse path I would also have to create an interface on DAO, and implement it on Model (or even Controller) when calling DAO? These methods that will be implemented would be the return? Because the return of DAO comes in a Thread for me and I can’t give "Return" directly.

  • Quiet, willing to help with whatever you need. Your controller then receives the DAO value, right? This is already happening?

  • Yes, Controller would receive the value of DAO with the return whether or not the user’s registration was certain. I’m using the Parse database, and when I use the method of registering it "opens in another Thread" and so I can’t give a "direct" Return, I would have to call some method for the Controller to know whether or not the user’s body was right. That’s why before I was passing the Controller by DAO parameter. Now I no longer know if I create an interface too or try some method that helps me wait for the Thread of the method of registering finish there yes give a return.

  • Yes, to "facilitate" I would have to do the same on the dao. In this case I see no problem having the interface only on the dao. In the controller you would only pass this interface directly to dao. No declaring this interface in the controller. Unless you need to manipulate the data.

  • Okay. So all that stuff you gave me would be the real MVC? We have the Activity that is the view, create a Controll and an interface within it, and in the Controll has the constructor that receives this interface and from it calls view methods. And in DAO we also have an interface that will serve this time for the controller causing the DAO to return the result to the controller through this interface? From what I understand it is this, I even managed to implement in this way.

  • Another thing, the View interface can count more than one method? Or it has to be 1 for each 1 interface?

  • Yes. The interface details are due to the need for callbacks. You can have more methods yes, as much as you need.

  • Thank you very much, congratulations on the time spent. I will take this learning to life, haha!

  • Great! I should have opened a chat, but I don’t know how to do it

  • Neither do I, haha. But I sent you an invitation from facebook, thanks again. Hugs.

Show 9 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.