Command template for passing application activity methods?

After reading Avoid memory leaks in the article @RomainGuy I realized that my current Android application is being prosecuted with an error transferring the application the main activity around. So whenever I can, I can simply replace this Activity.getApplicationContext () activity parameter.

But there are certain classes in my application that still need to run methods that can only be members of the core activity of the applications.

So I was thinking about using Command Pattern to get around this limitation.

The problem is that if we look at this example:

public class SomeCommandExecuableOnlyByActivity implements Command { public void execute(Object data) { doIt( ((MyActivity)data).getWindow() ); } } 

I run into a dead end again, requiring a pass around this activity (this time disguised as Object data).

How do I get out of this chicken and egg situation?

Is there a better way to approach this problem?

+7
source share
1 answer

I think that what you may be missing is the correct separation of problems. If you say that you need to transfer your main activity to other actions in order to trigger some functionality, then it seems to me that your application architecture has some fundamental design flaws.

Is it possible to use the command template here, I can’t say, but what I usually did was define those methods that require sharing, move them to a separate class and then either save a separate instance of this class in all actions that require this functionality , or if you need to provide the general state of an instance, create it in the global context of the application and provide it with a global access path (undesirable, but without a DI framework like RoboGuice, it is very difficult to implement DI on Android, since the constructor s are invalid.)

In my opinion, in a well-developed Android application, activity does not have business logic and provides only operations that change the state of the user interface. The user interface thread or any other calculations will be left to other classes. The Model-View-Presenter sample helps here extremely efficiently so that your code is structured to accountability.

Without giving us a deeper understanding of what you are trying to achieve, it is difficult to give concrete advice.

+3
source

All Articles