Last active
April 14, 2018 07:55
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
majestic