Skip to content

Instantly share code, notes, and snippets.

@silviorp
Last active April 14, 2018 07:55
Show Gist options
  • Save silviorp/3fa0e94babb4c75db8d4ce16972ad85f to your computer and use it in GitHub Desktop.
Save silviorp/3fa0e94babb4c75db8d4ce16972ad85f to your computer and use it in GitHub Desktop.
Base Activity providing ButterKnife binding, forcing layout Id register and abstracting showFragment call
// include package path here
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import butterknife.ButterKnife;
public abstract class BaseActivity extends AppCompatActivity {
// Abstract class used to get the activity Layout ID
protected abstract int getActivityLayout();
protected void configureActivity() {
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getActivityLayout());
ButterKnife.bind(this);
configureActivity();
}
// Method used to show a fragment in a Container View inside the Activity
protected void showFragment(int fragmentContainerId, BaseFragment fragment) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(fragmentContainerId, fragment);
transaction.addToBackStack(fragment.getClass().getName());
transaction.commit();
}
}
@CharlesAE
Copy link

majestic

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment