[英]Android: Can't call finish()
為什么不能在下面的代碼中調用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(它說:“無法解析構造函數”)。
您只能在Activity
上調用finish()
。 您發布的代碼不是Activity
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.