Android: 无法调用 finish()
Android: Can't call finish()
为什么我不能在下面的代码中调用 finish()
?
我也无法启动任何 Intent。
这是我的代码:
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);
}
}
启动一个 Intent 将用户发送回 LoginActivity(它将检查是否有用户)将是最好的解决方案,但问题是我无法启动一个 Intent(它说:"cannot resolve constructor").
您只能在 Activity
上呼叫 finish()
。您发布的代码不是 Activity
。
为什么我不能在下面的代码中调用 finish()
?
我也无法启动任何 Intent。
这是我的代码:
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);
}
}
启动一个 Intent 将用户发送回 LoginActivity(它将检查是否有用户)将是最好的解决方案,但问题是我无法启动一个 Intent(它说:"cannot resolve constructor").
您只能在 Activity
上呼叫 finish()
。您发布的代码不是 Activity
。