覆盖接口中的onBackPressed。

huangapple go评论84阅读模式
英文:

override onBackPressed from interface

问题

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

我想要重写它,以便每次都会检查活动是否是根活动,以便显示一个对话框,上面写着“你确定要退出吗?”

由于我在应用程序中多次使用它,我想创建一个接口并将其放在那里。

我对使用接口还不太熟悉,但我尝试了以下内容:

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上出现错误,错误信息如下:

无法解析方法'isTaskRoot',位于'customBackPressed'中

无法解析方法'onBackPressed',位于'Object'中

有人能否请解释一下如何解决这些问题?

谢谢

英文:

I use many times in my app the onBackPressed method.

I had like to override it so it will check every time if the activity is the root activity in order to show a dialog saying "are you sure you want to exit?"

Since I use it many times in my app, I thought to create an interface and place it there.

Im new with working with interfaces but I trying the following:

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();
        }
    }

}

My problem is that I get errors on isTaskRoot and onBackPressed saying the following:

Cannot resolve method 'isTaskRoot' in 'customBackPressed'

Cannot resolve method 'onBackPressed' in 'Object'

Can someone please explain to me how to work to solve this problems?

Thank you

答案1

得分: 0

You shouldn't represent behavior by interface method, you should represent it by the whole interface.

public interface OnBackPressedListener {
    void onBackPressedEvent()
}

然后,你应该将这个行为附加到 activity/fragment/view 的根部。例如:

public class MyActivity extends Activity implements OnBackPressedListener {
 
    // 初始化你的 activity 的字段
    
    private boolean isTaskRoot() {
        // 实现你的任务根方法
    }

    @Override
    public void onBackPressed() { // 这个方法是 activity 提供的 “默认实现”。
        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 接口不应该依赖于任何其他方法或行为。它的主要目的是提供一种能力,用于将类标记为能够对 onBackPressed 事件作出反应的类。

其中一个可能的用途是从 onBackPressedListener 继承你的片段,然后在父 Activity 收到后退按钮按下的通知时调用它们的 onBackPressedEvent 方法。

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

更新: 如果你想为所有继承 onBackPressedListener 的类提供现成的行为,你可以使用 default 关键字创建默认的接口方法。

public interface onBackPressedListener() {
    boolean isTaskRoot()

    default void onBackPressedEvent() {
        // 你的实现。
        // 它不应该包含调用这个接口未提供的方法。
        if (isTaskRoot()) // ...
    }
}

或者你可以创建一个 BaseActivity 类,在其中从接口中覆盖 onBackPressedEvent

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

然后你可以从它继承你的 activities。

public class MyActivity extends BaseActivity { ... }
英文:

You shouldn't represent behavior by interface method, you should represent it by whole interface.

public interface OnBackPressedListener {
    void onBackPressedEvent()
}

And then, you should attach this behavior to the activity/fragment/view root.
For example:

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();
            }
        }   
    }
}

Note that your onBackPressedListener interface should NOT depend on any other methods, behaviors. Its main purpose is to provide an ability to mark the class as the class, which can react to onBackPressed event.

One of the possible usages is to inherit your fragments from onBackPressedListener and then call their onBackPressedEvent methods when parent Activity is notified that back button was pressed.

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

UPD: If you want to provide ready-to-use behavior for all classes inheriting onBackPressedListener you can use default keyword to create default interface method.

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()) // ...
    }
}

OR you can create BaseActivity class, in which you will override onBackPressedEvent from interface

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

And then you inherit your activities from it
public class MyActivity extends BaseActivity { ... }

答案2

得分: -1

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

你的方法:

'isTaskRoot'

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

customBackPressed

没有包含任何具有那个名称的方法。

我认为有一种通过接口提供方法访问权限的方式……你应该尝试找出如何做。

英文:

I'm not sure... but I think it's wrong to call a method from another class in a totally different class without importing or referencing it.

your method:

'isTaskRoot'

is from another class that you didn't give information about, and your class:

customBackPressed

does not contain any method with that name.

I think there's a way to give access to methods through the interface... You should try and find how.

huangapple
  • 本文由 发表于 2020年8月14日 18:19:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/63410912.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定