Search code examples
androidandroid-activitymvp

Use MVP in Project,Activity.finish() doesn't work


In Activity,save() method will be call in toolbar's onOptionsItemSelected():

 @Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()){

        case R.id.action_save:
            save();
            break;
    }
    return super.onOptionsItemSelected(item);
}

save() method:

@Override
public void save() {
    Snackbar.make(mContentView,"save",Snackbar.LENGTH_SHORT).show();
    presenter.saveTask(mEditText.getText().toString(), mNpAddScore.getValue());
}

saveTask() in presenter:

@Override
public void saveTask(String content, int addScore) {
    CuteLog.tLog("content:" + content + "content.len:" +   content.length()+",addScore:" + addScore);
    if (content == null || content.length() == 0){
        view.remindNullInput();
        return;
    }

    modle.addTask(content, addScore+"");
    view.closeSelf();
    CuteLog.tLog("saveTask!!!!");
}

and view.closeSelf() will be called,view is the activity in begining. closeSelf():

@Override
public void closeSelf() {
    CuteLog.tLog("finish");
    finish();
    CuteLog.tLog("finish2");
    return;
}

BUT,nonsensically,the activity doesn't finish!! And "finish","finish2" be printed in logcat.

Why?


Solution

  • Finish() 
    

    will just queue the operation in the main thread to finish the activity but its not a synchronous operation :)

    You can listen to its events with the onDestroy and checking if isFinishing() methods.

    The best ways as usual is play and understand the lifecycle of activities.

    http://developer.android.com/intl/es/training/basics/activity-lifecycle/index.html