Skip to content

Instantly share code, notes, and snippets.

@silviorp
Last active April 14, 2018 07:54
Show Gist options
  • Save silviorp/7e8d698713614cd85081af59dbd880d2 to your computer and use it in GitHub Desktop.
Save silviorp/7e8d698713614cd85081af59dbd880d2 to your computer and use it in GitHub Desktop.
Example of activity using CustosmBottomNavigationBar registering menu items and controlling the back stack
// include package path here
import android.support.v4.app.FragmentManager;
import android.widget.Toast;
import br.com.intelize.onhome.presenter.R;
import br.com.intelize.onhome.presenter.components.CustomBottomNavigationView;
import br.com.intelize.onhome.presenter.view.base.BaseActivity;
import br.com.intelize.onhome.presenter.view.fragment.BillsFragment;
import br.com.intelize.onhome.presenter.view.fragment.SettingsFragment;
import br.com.intelize.onhome.presenter.view.fragment.ShoppingFragment;
import br.com.intelize.onhome.presenter.view.fragment.SummaryFragment;
import br.com.intelize.onhome.presenter.view.fragment.TasksFragment;
import butterknife.BindView;
public class MainActivity extends BaseActivity {
private Boolean ACTIVITY_BACKPRESSED_EXIT = false;
@BindView(R.id.bottom_bar_view)
CustomBottomNavigationView bottomBarView;
@Override
protected int getActivityLayout() {
return R.layout.activity_main;
}
protected void configureActivity() {
configureMenuNavigation();
configureMainFragment();
}
@Override
protected void onResume() {
super.onResume();
ACTIVITY_BACKPRESSED_EXIT = false;
}
private void configureMenuNavigation() {
bottomBarView.registerFragmentToBarItem(R.id.item_summary, SummaryFragment.class, () -> showFragment(R.id.fragment_container, SummaryFragment.newInstance()));
bottomBarView.registerFragmentToBarItem(R.id.item_bills, BillsFragment.class, () -> showFragment(R.id.fragment_container, BillsFragment.newInstance()));
bottomBarView.registerFragmentToBarItem(R.id.item_tasks, TasksFragment.class, () -> showFragment(R.id.fragment_container, TasksFragment.newInstance()));
bottomBarView.registerFragmentToBarItem(R.id.item_shopping, ShoppingFragment.class, () -> showFragment(R.id.fragment_container, ShoppingFragment.newInstance()));
bottomBarView.registerFragmentToBarItem(R.id.item_settings, SettingsFragment.class, () -> showFragment(R.id.fragment_container, SettingsFragment.newInstance()));
}
private void configureMainFragment() {
showFragment(R.id.fragment_container, new SummaryFragment());
}
@Override
public void onBackPressed() {
FragmentManager fragmentManager = getSupportFragmentManager();
int backStackCount = fragmentManager.getBackStackEntryCount();
if (backStackCount > 1) {
if (fragmentManager.popBackStackImmediate()) {
bottomBarView.setSelectedByFragmentManager(fragmentManager);
}
ACTIVITY_BACKPRESSED_EXIT = false;
} else {
if(!ACTIVITY_BACKPRESSED_EXIT){
Toast.makeText(this, getResources().getString(R.string.activity_toast_backpressed), Toast.LENGTH_LONG).show();
ACTIVITY_BACKPRESSED_EXIT = true;
} else {
moveTaskToBack(true);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment