简体   繁体   English

使用单个类的方法进行多个活动

[英]Multiple activities using methods from a single class

This is more of a question where I'm trying to see if my thinking is correct before I start coding something that winds up not working. 这是一个更大的问题,在尝试编写最终无法正常工作的代码之前,我试图查看自己的想法是否正确。 Let me see if I can explain what I'm trying to do first. 让我看看能否先解释一下我要做什么。 My main issue is wanting to know if I'm on the right track or not. 我的主要问题是想知道我是否在正确的轨道上。

I'm creating a game using the page viewer and fragments so I can have a tabbed layout. 我正在使用页面查看器和片段创建游戏,因此可以使用选项卡式布局。 That's not a problem. 那不是问题。 Where I'm getting confused and turned around is that I have multiple activities, each handling a different part of the game so that I avoid having too much on the screen at any one time. 我感到困惑和困惑的地方是我有多个活动,每个活动处理游戏的不同部分,因此我避免在任何时候都在屏幕上玩太多游戏。 I had wanted to have all of the game methods in a single class and have each activity pull from it. 我本来希望将所有游戏方法都​​放在一个类中,然后从中进行每个活动。 Can I do so if the class is private? 如果班级是私人的,我可以这样做吗? I assume if I had a GAME class for example that I would have to instantiate a particular version of it somewhere but if I did, how can I be sure that everything is pulling from that same instance? 我假设,例如,如果我有一个GAME类,则必须在某个地方实例化该类的特定版本,但是如果我这样做了,如何确定所有东西都来自同一实例?

Another approach I had thought of was basically just making one game class that did everything and then just spit out the variable values to the various activities as needed which would then update the UI. 我想到的另一种方法基本上是制作一个可以完成所有工作的游戏类,然后根据需要将变量值吐给各种活动,然后更新UI。 Would that be a better approach? 那会是更好的方法吗? I think if I do it this way I can avoid having to pass data directly from one activity to the next. 我认为,如果我这样做,就可以避免将数据直接从一个活动传递到下一个活动。 I can just have them all pull whatever values they need directly from the class. 我可以让他们全部直接从类中获取他们需要的任何值。

Does any of this make any sense or am I way out in left field somewhere? 这有什么意义吗?还是我在左边的某个地方走出来? Any direction to go in to start would be helpful as I've been going around for days trying to chart some kind of course. 任何开始的方向都会有所帮助,因为我已经尝试了数天,试图绘制某种路线。

It sounds like you want to put all your game logic into a Game class and have all of your Activities access that single Game instance. 听起来您想将所有游戏逻辑都放入Game类中,并让所有Activity都可以访问该单个Game实例。 The answer is yes, that's probably the right approach and a simpler approach then trying to get your Activities to pass data around via Intents. 答案是肯定的,这可能是正确的方法,然后是一种简单的方法,然后尝试使您的活动通过Intent传递数据。

You can use a singleton pattern to get access to the same instance everywhere. 您可以使用单例模式在任何地方访问同一实例。 (Something like this) (这样的事情)

public class Game {
    private static final Game INSTANCE = new Game();

    private Game() {}

    public static Game getInstance() {
        return INSTANCE;
    }
}

Or if you're using a dependency injection framework like Dagger, you can just mark the Game provider as providing a singleton. 或者,如果您使用的是Dagger之类的依赖项注入框架,则可以仅将Game提供程序标记为提供单例。 (If you don't know what this is, just use the singleton pattern above for now) (如果您不知道这是什么,请暂时使用上面的单例模式)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM