2

I am developing a multi level game, where each level is a new activity.

I want to know, if i change the activity like

Intent myIntent = new Intent(getBaseContext(), Level3.class);
                startActivity(myIntent);

The memory used for Level 1 and 2 is cleared?

If not, how can I clear everything from previous level activities so the phone uses just the memory just for the current activity ?

braX
  • 11,506
  • 5
  • 20
  • 33
user1452079
  • 118
  • 10
  • you should finish before activity for clear memory .use this.finish() –  Jun 23 '13 at 08:43

3 Answers3

0

You need to call finish() for the activity (or activities) that you no longer want to be active. You can simply call it right after starting the new activity:

Intent myIntent = new Intent(getBaseContext(), Level3.class);
startActivity(myIntent);
finish();

Otherwise, the previous activity will remain on the activity stack.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
0

since you are using an activity/level design, just add a check if your activity is finishing in your onPause method, and null all your references to the current level, that way the GC will know that your level object should be released, and it will release it as soon as possible.

@Override
public void onPause(){
      super.onPause();
      if (isFinishing()){
         levelObject = null;
}

}

Mr.Me
  • 9,192
  • 5
  • 39
  • 51
0

I would not recommend you to create Activity for each of your game level. Would be better to create some Controller that will initializate you game levels in one Activity. And of cource it must have some methods to clear memmory from last stage, something like this :

    class StageManager
{
    public Stage curStage;

    public initStage(Stage stage)
    {
        //init stage here
        curStage = stage;
        stage.init();
    }

    public clearStage()
    {
        //do some clearing staff
        curStage .clear();
    }
}

    abstract class Stage
{
    public abstract init();
    public abstract clear();
}

    abstract class FirstStage extends Stage
{
    //....
} 

    abstract class SecondStage extends Stage
{
    //....
} 

In Activity :

StageManager stageManager = new StageManager();

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.your_view);

    stageManager.init(new FirstStage());
}

@Override
public void onClick(View theView)
{
    int id = theView.getId();

    if (id == R.id.btnNextLevel) {
        stageManager.clear();
        stageManager.init(new SecondStage());
    }
}

Instead of your custom manager, you can use fragmets :

In both ways - fragments or yout own manager you will seperate different stages logic to different classes.

Youd don't need to create another Activity to seperate yours 1000+ lines code. Just use one of Stage or Stratagy desing patters.

And if you still want to use Activities just do something like this :

Intent myIntent = new Intent(getBaseContext(), Level3.class);
startActivity(myIntent);
finish();

and in onDestroy() :

    @Override
protected void onDestroy()
{
    //here you must clear all data that were used in this Stage (Activity) like this :

    clearMemmory();
    super.onDestroy();
}

private void clearMemmory()
{
    if(stageData!=null)
    {
        stageData.clear();
        stageData =null;
    }
}

or clear memmory directly before opening another Stage, something like :

clearMemmory();

Intent myIntent = new Intent(getBaseContext(), Level3.class);
startActivity(myIntent);

finish();

Best wishes.

Yakiv Mospan
  • 8,174
  • 3
  • 31
  • 35
  • Each level is a very different game.. so is hard to have 1000+ line in 1 activity.. that's why i like using more.. – user1452079 Jun 23 '13 at 10:35