java - Android:无法调用 finish()

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

为什么我无法在下面的代码中调用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 (它说:“无法解析构造函数”)。

最佳答案

您只能对 Activity 调用 finish()。您发布的代码不是Activity

关于java - Android:无法调用 finish(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57350655/

相关文章:

java - Hibernate List<String> Java 持久性 1.0

android - 如何找到我们是否在 Dart (Flutter) 中运行单元测试

android - 在多次点击后延迟一个 Intent ?

java - Sherlock Activity 中的 Intent.FLAG_ACTIVITY_CLEAR_TOP 未达到目的

java - android:如何将LinkedHashMap发送到不固定大小的URL

java - WebClient - Jira Rest api,出现意外结果

java - 禁用 Checkstyle 规则以匹配模式的类名

安卓 API-23 : InetAddressUtils replacement

android - 从 RecyclerView 中删除一个项目(ViewHolder 有 onClick 但适配器有数据集)

android - 从 Android Intent 打开 Ionic (Capacitor) 应用程序中的特定页面