从界面覆盖 onBackPressed

override onBackPressed from interface

我在我的应用程序中多次使用 onBackPressed 方法。

我想重写它以便每次检查 activity 是否是根 activity 以显示一个对话框“你确定要退出吗?”。 =15=]

因为我在我的应用程序中多次使用它,所以我想创建一个界面并将其放置在那里。

我是使用接口的新手,但我尝试了以下操作:

public interface App_Interfaces {

    void BackPressed(Context contect);

}

class customBackPressed implements App_Interfaces{
    @Override
    public void BackPressed(Context context) {

        if(isTaskRoot()){

            final Dialog dialog = new Dialog( context );
            dialog.setContentView( R.layout.are_you_sure );
            dialog.getWindow().setBackgroundDrawable( new ColorDrawable( Color.TRANSPARENT ) );
            
            if (!((Activity) context).isFinishing()) {
                dialog.show();
            }
            
        }else{
            super.onBackPressed();
        }
    }

}

我的问题是我在 isTaskRootonBackPressed 上收到以下错误:

Cannot resolve method 'isTaskRoot' in 'customBackPressed'

Cannot resolve method 'onBackPressed' in 'Object'

有人可以向我解释如何解决这个问题吗?

谢谢

我不确定...但我认为在完全不同的 class 中调用另一个 class 的方法而不导入或引用它是错误的。

你的方法:

'isTaskRoot'

来自另一个你没有提供信息的 class,你的 class:

customBackPressed

不包含任何具有该名称的方法。

我认为有一种方法可以通过接口访问方法...您应该尝试找出方法。

你不应该用接口方法来表示行为,你应该用整个接口来表示它。

public interface OnBackPressedListener {
    void onBackPressedEvent()
}

然后,您应该将此行为附加到 activity/fragment/view 根。 例如:

public class MyActivity extends Activity implements OnBackPressedListener {

    // Initialize your activity's fields
    
    private boolean isTaskRoot() {
    // implement your task root method
    }

    @Override
    public void onBackPressed() { // This method is provided by activity "from the box".
        onBackPressedEvent();
    }

    @Override
    public void onBackPressedEvent() {
        if(isTaskRoot()) {
            final Dialog dialog = new Dialog(this);
            dialog.setContentView( R.layout.are_you_sure );
            dialog.getWindow().setBackgroundDrawable( new ColorDrawable( Color.TRANSPARENT ) );
        
            if (!this.isFinishing()) {
                dialog.show();
            }
        }   
    }
}

请注意,您的 onBackPressedListener 界面不应依赖于任何其他方法、行为。它的主要目的是提供一种将 class 标记为 class 的能力,它可以对 onBackPressed 事件做出反应。

一种可能的用法是从 onBackPressedListener 继承您的片段,然后在通知父 Activity 按下后退按钮时调用它们的 onBackPressedEvent 方法。

@Override
public void onBackPressed() {
    // ...
    if (currFragment instanceof onBackPressedListener) {
        currFragment.onBackPressedEvent();
    }
    // ...
}

更新: 如果你想为所有继承 onBackPressedListener 的 class 提供 ready-to-use 行为,你可以使用 default用于创建默认接口方法的关键字。

public interface onBackPressedListener() {
    boolean isTaskRoot()

    default void onBackPressedEvent() {
        // Your implementation. 
        // It should not contain calls of methods, which are not presented by this interface. 
        if (isTaskRoot()) // ...
    }
}

或者您可以创建 BaseActivity class,在其中您将从接口

覆盖 onBackPressedEvent
public class BaseActivity extends Activity implements OnBackPressedListener { 
    @Override
    public void onBackPressed() {
    ...
    }
}

然后你从它那里继承你的活动 public class MyActivity extends BaseActivity { ... }