The easiest way is to use runnable see how
//this function can take function as parameter private void doSomethingOrRegisterIfNotLoggedIn(Runnable r) { if (isUserLoggedIn()) r.run(); else new ViewDialog().showDialog(MainActivity.this, "You not Logged in, please log in or Register"); }
Now let's see how I can pass any function to it (I will not use a lambda expression)
Runnable r = new Runnable() { @Override public void run() { startActivity(new Intent(MainActivity.this, AddNewPostActivity.class)); } }; doSomethingOrRegisterIfNotLoggedIn(r);
pass another function
Runnable r = new Runnable() { @Override public void run() { if(!this.getClass().equals(MyProfileActivity.class)) { MyProfileActivity.startUserProfileFromLocation( MainActivity.this); overridePendingTransition(0, 0); } } }; doSomethingOrRegisterIfNotLoggedIn(r);
here it is. happy big thinking ...
Basheer AL-MOMANI
source share