完成()有时不起作用

时间:2014-12-03 23:13:24

标签: android activity-finish

我有一个不同的Level Classes,我从一个函数调用它。 问题是有时Endscreen.Class没有关闭......但它只发生在Level5及以上。

我在Endscreen.class中的功能:

public void NextLevel(View v){
    switch(currentLevel){
        case 1:
            Intent nLvl1 = new Intent (this, Level2.class);
            startActivity(nLvl1);
            finish ();
            break;
        case 2:
            Intent nLvl2 = new Intent (this, Level3.class);
            startActivity(nLvl2);
            finish ();
            break;
        case 3:
            Intent nLvl3 = new Intent (this, Level4.class);
            startActivity(nLvl3);
            finish ();
            break;
        case 4:
            Intent nLvl4 = new Intent (this, Level5.class);
            startActivity(nLvl4);
            finish ();
            break;
        case 5:
            Intent nLvl5 = new Intent (this, Level6.class);
            startActivity(nLvl5);
            finish ();
            break;
        case 6:
            Intent nLvl6 = new Intent (this, Level7.class);
            startActivity(nLvl6);
            finish ();
            break;
        case 7:
            Intent nLvl7 = new Intent (this, Level8.class);
            startActivity(nLvl7);
            finish ();
            break;
        case 8:
            Intent nLvl8 = new Intent (this, Level9.class);
            startActivity(nLvl8);
            finish ();
            break;
        case 9:
            Intent nLvl9 = new Intent (this, LevelSelect.class);
            startActivity(nLvl9);
            finish ();
            break;
    }
}

2 个答案:

答案 0 :(得分:1)

轻松解决此问题。对于每个意图,请添加以下内容:

intentName.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

这将以您希望的方式摆脱当前活动finish()

您可以阅读有关此here的文档。

答案 1 :(得分:0)

我用以下方法解决了问题:

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);