The best solution of method dependency

Viewed 72

I am wondering if there is any solution for method dependency of this object and the supper object. I am now working in android, and overriding the onBackPressed method.

I have created a class called ActivityContainer, used to handle universal behaviors. the logic of the onBackPressed of this class is

  1. Close the menu if the menu is currently opening
  2. Prompt if going to close the app, yes to close, no or no action to stay

Then, I have an other class called FragmentActivityContainer extends ActivityContainer, used to handle universal behaviors that used in fragment. the logic of the onBackPressed of this class id

  1. Close the menu if the menu is currently opening
  2. Back the previous fragment if changed fragments in content area
  3. Prompt if going to close the app, yes to close, no or no action to stay

the dependency is messy. Let's take a look for ActivityContainer first

@Override
public void onBackPressed(){
    if(this.menu != null && this.menu.isShowing()){
        this.menu.close();
    }  else {
        this.promptQuite();
    } 
} 

nice and clear, works fine.

Let's have a look for FragmentActivityContainer

    @Override
    public void onBackPressed(){
        if(this.menu != null && this.menu.isShowing()){
            this.menu.close();
        }  else if(this.fragmentManager.getBackStackEntryCount() > 0){
            super.getParent().onBackPressed();
        } else{
            this.promptQuite();
        }
    }

it works, but eagerly. I would like to use super.onBackPressed() in FragmentActivityContainer but since back to previous fragment is in the middle between menu close and prompt quite, there is no way for me to do that. What is the best solution to solve this dependency problem?

0 Answers
Related