调用finish()后为什么不停止android活动?

时间:2012-07-14 12:46:54

标签: android android-intent progressdialog activity-finish

我有一个名为MyProgressDialog的Activity,它包含一个ProgressDialog。通过意图在主活动中调用此ScreenProgressDialog活动:

       if(msg.what == SET_PROGRESS){

            intent.putExtra("action", "set");
            ...

            intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);

            startActivity(intent);
        }
        else if(msg.what == SHOW_PROGRESS){             


            intent.putExtra("action", "show");
            ...

            intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);

            startActivity(intent);              
        }
        else if(msg.what == HIDE_PROGRESS){

            intent.putExtra("action", "hide");

            intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

            startActivity(intent);

        }

以下是MyProgressDialog活动:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Log.e("screenPD", "spd created");

    extras = getIntent().getExtras();



    pd = new ProgressDialog(this);

    ...setting the pd...        

    pd.show();  

    Log.e("screenPD", "spd shown");
}

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);

    setIntent(intent);

    int newTitle = intent.getExtras().getInt("title");

    if (intent.getExtras().getString("action").equals("set")){
        pd.set methods...
        pd.show();

        Log.e("DialogSET", "DialogSET "+intent.getExtras().getInt("progress"));
    }
    else if (intent.getExtras().getString("action").equals("show")){
        pd.set methods...
        pd.show();

        Log.e("DialogSHOW", "DialogSHOW "+progress);

    }
    else if (intent.getExtras().getString("action").equals("hide")){
        pd.dismiss();
        this.finish();
        Log.e("DialogHIDE", "DialogHIDE");
        return;
    }
}

@Override
public void onDestroy() {
    super.onDestroy();
    Log.e("screenPD", "destroyed");     
}

这是LogCat:

DialogHIDE(2615): DialogHIDE
screenPD(2615): spd created
screenPD(2615): spd shown
screenPD(2615): destroyed

所以第三个意图开始,调用finish();返回;并启动Onreate方法,显示一个新的ProgressDialog。调用onDestroy,但ProgressDialog不会从屏幕上隐藏。在finish()方法之后,活动将被关闭。问题出在哪儿?谢谢!

2 个答案:

答案 0 :(得分:0)

没有错误。调用finish()的方法将用完,完成后控件返回Android

答案 1 :(得分:0)

在startactivity方法之后,调用finish()来完成当前活动。在开始完成上一个活动的新活动之后。