Is there a limit for startActivity new Intent? After 50 clicks you can't open a new activity

61 Views Asked by At

I am making game with different levels with each level in a new activity. When user click startActivity new intent for 50 times during the game it won't open a new activity. e.g level 1= activity 1 so user wont be able to open level 51 = activity 51. If you move between activities 50 times you wont be able to move 51st time. I have checked this error on a different app as well where you can't switch between activities more than 50 times. Any idea why there is a limit and how i can solve it. Help would be appreciated.

startActivity(new Intent(this,Level50.class));
2

There are 2 best solutions below

1
404NotFound On

Theoretically there is no such limit. But this might be happening because there are so many activities in the stack. You can try these flags to remove previous Activity from the stack. As you are developing game, I am assuming you will not be requiring to go previous level once user has completed it.

Intent intent = new Intent(this, ActivityB.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | 
Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
0
Davit Hovhannisyan On

Here's a simplified example using fragments:

public class GameActivity extends AppCompatActivity {
    private int currentLevel = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game);
        loadLevel(currentLevel);
    }

    private void loadLevel(int level) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.fragment_container, LevelFragment.newInstance(level));
        transaction.commit();
    }


    public void moveToNextLevel() {
        currentLevel++;
        if (currentLevel <= 50) {
            loadLevel(currentLevel);
        } else {
            
        }
    }
}