1

First, I am an android rookie, so my solution ways can be found awkward, and i am open to suggestions. I am trying to create a game manager object that handles all transitions between activities. And my purpose is that while in an activity, menuOut method will call the changeActivity method of GameManager object with nextActivity argument and changeActivity will start that Activity. I am getting errors consistently, and did not find a solution.

Here is my source codes: GameManager:

public class GameManager{
    public SplashScreen splash = new SplashScreen();
    public MainScreen main = new MainScreen();
    public LoadingScreen load = new LoadingScreen();

    Context tempContext;

    public GameManager(Context base) {
        super();
        tempContext = base;
    }

    public void start(){
        createScreens();
        Intent intent = new Intent(tempContext, splash.getClass());
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        tempContext.startActivity(intent);
    }

    public void createScreens() {
        //here is the method that i am trying to find a solution

        ((SplashScreen)splash.getContext()).setGameClass(this);
        ((MainScreen)main.getContext()).setGameClass(this);
        ((LoadingScreen)load.getContext()).setGameClass(this);
    }

    public void changeMenu(MenuType nextMenu, MenuType previousMenu){
        Intent intent2;
        switch(nextMenu){
        case MAIN_SC:
            tempContext = main.getContext();
            intent2.setClass(tempContext, main.getClass());
            intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            tempContext.startActivity(intent2);
        case GAME_LOADING_SC:
            tempContext = load.getContext();
            intent2.setClass(tempContext, load.getClass());
            intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            tempContext.startActivity(intent2);
        default:
            break;
        }
    }
}

And here is SplashScreen activity:

public class SplashScreen extends Activity {
    public Context context = this;
    public GameManager gameman;
    private static final int SPLASH_DURATION = 4000;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        splash();
        menuOut();
    }

    public Context getContext() {
        return this;
    }

    public void splash() {
         LinearLayout ll = new LinearLayout(this);
         ll.setOrientation(LinearLayout.HORIZONTAL);
         ll.setBackgroundResource(R.drawable.game_loop_splash);

         setContentView(ll);

         Handler handler = new Handler();

         // run a thread after 2 seconds to start the home screen
         handler.postDelayed(new Runnable() {
             @Override
             public void run() {
                 finish();
             }
         }, SPLASH_DURATION);
    }

    public void setGameClass(GameManager game){
        gameman = game;
    }

    private void menuOut(){
        gameman.changeMenu(MenuType.GAME_LOADING_SC, MenuType.GAME_SPLASH_SC);
        this.onDestroy();
    }
}

I can not return to the GameManager and call the changeMenu method. I am very exhausted to get null pointer exceptions. Any idea?

Edward Falk
  • 9,991
  • 11
  • 77
  • 112

1 Answers1

1

From what I read, you are trying to implement a singleton pattern. There are two ways I'd recommend to do that on android:

  1. Extend the Application class, register your class in the manifest and use getApplication() in your activities to get access to it:

    // In MyApplicationSubclass.java:
    public final class MyApplicationSubclass extends Application {
      /* ... */
      public void myMethod() {
        // insert some code here
      }
      /* ... */
    }
    
    // From your Activity:
    ((MyApplicationSubclass) this.getApplication()).myMethod();
    
  2. Use a "normal" java singleton pattern, e.g. use a private constructor and keep one static instance within your GameManager class (this is the way the Android docs recommend, but I personally prefer the first way when having something that is logically bound to the Application).


Also, if you're only using your central class to do static stuff, you can just mark all its method as static and access them directly. Transfer Context objects as parameters to these methods, and you should be able to start activities without any static variables (which are sometimes hard to implement properly in Android, as your VM might get restarted from time to time).

dst
  • 3,307
  • 1
  • 20
  • 27
  • Actually I can't get your Application solution. Can you explain it a bit detailed? – Talat Tekbudak Aug 19 '13 at 01:04
  • @TalatTekbudak hope the example helps a bit? I also linked the android docs. – dst Aug 19 '13 at 01:23
  • Don't worry about the Application solution too much. Basically, you extend the **Application** class to add your persistent state to it, and then arrange through the manifest to have your subclass used instead of the default **Application** class. I've done it that way on occasion, but the singleton pattern works too. See my other post for more info on Singleton: http://stackoverflow.com/questions/13670862/setting-a-value-in-one-class-and-retrieving-from-another-class-in-java-android/13673178#13673178 – Edward Falk Aug 19 '13 at 01:42