Last active
August 15, 2016 05:24
-
-
Save li2/71990eec8eea0edcc483eafd6083a0f0 to your computer and use it in GitHub Desktop.
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
/** | |
* Created by weiyi.li on 2/19/16 | |
* li2.me/2016/08/make-a-reusable-ui-in-android-app-development.html | |
*/ | |
public abstract class BasicActivity extends AppCompatActivity { | |
protected TextView mActionTitleView; | |
protected TextView mActionBackBtn; | |
protected Button mActionCloseBtn; | |
protected Fragment mContentFragment; | |
@Override | |
protected void onCreate(@Nullable Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_basic); | |
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); | |
setupActionBar(); | |
setupContentFragment(); | |
inflateOperationToolbar(); | |
} | |
/** Subclass must override this method to create content fragment. */ | |
protected abstract Fragment createContentFragment(); | |
/** Subclass should override this method to inflate bottom toolbar layout, if it has a bottom toolbar. */ | |
protected ViewStub inflateOperationToolbar() { | |
ViewStub stub = (ViewStub) findViewById(R.id.operationToolbarViewStub); | |
return stub; | |
} | |
/** Subclass should override this method to set actionbar title. */ | |
protected String getActionBarTitle() { | |
return ""; | |
} | |
/** | |
* Callback when ActionBar's Back & Close button clicked. | |
* Subclass may override these two methods to do more things. | |
*/ | |
protected void onActionBackClicked() { | |
finish(); | |
} | |
protected void onActionCloseClicked() { | |
// Clear all activities in this App. | |
finishAffinity(); | |
} | |
/** Setup ActionBar with support toolbar. */ | |
private void setupActionBar() { | |
Toolbar topActionbar = (Toolbar) findViewById(R.id.topActionbar); | |
setSupportActionBar(topActionbar); | |
mActionTitleView = (TextView) topActionbar.findViewById(R.id.topActionTitle); | |
mActionTitleView.setText(getActionBarTitle()); | |
mActionBackBtn = (TextView) topActionbar.findViewById(R.id.topActionBack); | |
mActionBackBtn.setOnClickListener(new DebouncedOnClickListener() { | |
@Override | |
public void onDebouncedClick(View v) { | |
onActionBackClicked(); | |
} | |
}); | |
mActionCloseBtn = (Button) topActionbar.findViewById(R.id.topActionClose); | |
mActionCloseBtn.setOnClickListener(new DebouncedOnClickListener() { | |
@Override | |
public void onDebouncedClick(View v) { | |
onActionCloseClicked(); | |
} | |
}); | |
} | |
/** Setup fragment. */ | |
private void setupContentFragment() { | |
FragmentManager fm = getSupportFragmentManager(); | |
Fragment fragment = fm.findFragmentById(R.id.contentFragmentContainer); | |
if (fragment == null) { | |
fragment = createContentFragment(); | |
if (fragment != null) { | |
fm.beginTransaction().add(R.id.contentFragmentContainer, fragment).commit(); | |
} | |
} | |
mContentFragment = fragment; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment