Changing activity in android clears the memory needed for the previous activities?

前端 未结 3 1637
青春惊慌失措
青春惊慌失措 2021-01-15 16:04

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(ge         


        
相关标签:
3条回答
  • 2021-01-15 16:32

    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;
    }
    

    }

    0 讨论(0)
  • 2021-01-15 16:47

    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 :

    • http://developer.android.com/training/basics/fragments/creating.html
    • http://developer.android.com/training/basics/fragments/fragment-ui.html

    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.

    0 讨论(0)
  • 2021-01-15 16:51

    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.

    0 讨论(0)
提交回复
热议问题