Android:无法调用finish()

时间:2019-08-04 22:36:40

标签: java android android-intent activity-finish system.exit

为什么我不能在下面的代码中调用finish()? 我也无法启动任何意图。

这是我的代码:

public class EditProfilePresenter<V extends EditProfileView> extends PickImagePresenter<V> {
    protected Profile profile;
    protected ProfileManager profileManager;

    protected EditProfilePresenter(Context context) {
        super(context);
        profileManager = ProfileManager.getInstance(context.getApplicationContext());
    }

    public void loadProfile() {
        ifViewAttached(BaseView::showProgress);
        profileManager.getProfileSingleValue(getCurrentUserId(), new OnObjectChangedListenerSimple<Profile>() {
            @Override
            public void onObjectChanged(Profile obj) {
                profile = obj;
                ifViewAttached(view -> {
                    if (profile != null) {
                        view.setName(profile.getUsername());
                        if (profile.getPhotoUrl() != null) {
                            view.setProfilePhoto(profile.getPhotoUrl());
                        }
                    }
                    view.hideProgress();
                    view.setNameError(null);
                });
            }
        });
    }

    public void attemptCreateProfile(Uri imageUri) {
        if (checkInternetConnection()) {
            ifViewAttached(view -> {
                view.setNameError(null);
                String name = view.getNameText().trim();
                boolean cancel = false;
                if (TextUtils.isEmpty(name)) {
                    view.setNameError(context.getString(R.string.error_field_required));
                    cancel = true;
                } else if (!ValidationUtil.isNameValid(name)) {
                    view.setNameError(context.getString(R.string.error_profile_name_length));
                    cancel = true;
                }
                if (!cancel) {
                    view.showProgress();
                    profile.setUsername(name);
                    createOrUpdateProfile(imageUri);
                }
            });
        }
    }

    private void createOrUpdateProfile(Uri imageUri) {
        profileManager.createOrUpdateProfile(profile, imageUri, (boolean success) -> {
            ifViewAttached(view -> {
                view.hideProgress();
                if (success) {
                    onProfileUpdatedSuccessfully();
                    // ----- HERE -----
                    System.exit(0);
                    // I can't call finish(), but can call System.exit().
                } else {
                    view.showSnackBar(R.string.error_fail_create_profile);
                }
            });
        });
    }

    protected void onProfileUpdatedSuccessfully() {
        ifViewAttached(BaseView::finish);
    }
}

启动将用户发送回LoginActivity的Intent(这将检查是否有用户)将是最佳解决方案,但是问题是我无法启动Intent(它说:“无法解析构造函数“)。

1 个答案:

答案 0 :(得分:2)

您只能在finish()上呼叫Activity。您发布的代码不是Activity