I have a redefined function that is the same on all screens ...

I can not guess, is it possible to somehow make a refactor, so as not to repeat it on each activator?

And what about the drawer variable in this case?

 @Override public void onBackPressed() { if (drawer.isDrawerOpen()) { drawer.closeDrawer(); } else { if (backPressedQ == 1) { backPressedQ = 0; super.onBackPressed(); overridePendingTransition(R.anim.open_main, R.anim.close_next); } else { backPressedQ++; Toast.makeText(this, "Press again to exit", Toast.LENGTH_SHORT).show(); } //Обнуление счётчика через 5 секунд final Handler handler = new Handler(); handler.postDelayed(new Runnable() { @Override public void run() { // Do something after 5s = 5000ms backPressedQ = 0; //checkNew(); } }, 5000); } } 

    1 answer 1

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

    Then we inherit all the necessary necessary activites from it:

     class FirstActivity extends BaseActivity { ... } class SecondActivity extends BaseActivity { ... } 
    • Add a little explanation, I think, just this code fragment is not enough for the author to understand - Vladyslav Matviienko
    • one
      @metalurgus and what description to add ... What do you think, is it possible in this case how to correctly make a refactor or do you need to duplicate it on each activation? - Aleksey Timoshchenko pm