NavigationViewManager


Source link: https://github.com/rubensousa/NavigationViewManager

NavigationManager

A simple helper library to separate navigation logic from your MainActivity and reduce boilerplate.

It also includes ActionMode flow control when the NavigationView is shown/hidden.

Check the sample app.

Minimum API: 14

Build

Add the following to your build.gradle:

dependencies {

  compile 'com.github.rubensousa:navigationmanager:1.2.1' 
}

How to use

#####1. Create your NavigationManager class that extends from NavigationManager.

#####2. Implement getDefaultItem to show a default fragment on first start.

@Override public int getDefaultItem() {

  return R.id.nav_import; 
}
3. Implement createFragment to replace your content fragments.
@NonNull @Override public Fragment createFragment(@IdRes int item) {

  switch (item) {

case R.id.nav_import:

 return new ImportFragment();

case R.id.nav_gallery:

 return new GalleryFragment();

default:

 return new DummyFragment();

  
}
 
}

#####4. Add the following to your MainActivity:

private NavigationManager mNavigationManager;  @Override protected void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

  setContentView(R.layout.activity_main);

  NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);

  DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);

mNavigationManager = new NavigationViewManager(getSupportFragmentManager(),

 navigationView, drawer, R.id.containerLayout);

mNavigationManager.init(savedInstanceState, getIntent());
 
}
  // This is useful to navigate when you receive an intent // from a push notification @Override protected void onNewIntent(Intent intent) {

  super.onNewIntent(intent);

  mNavigationManager.navigateWithIntent(intent);
 
}
  @Override protected void onSaveInstanceState(Bundle outState) {

  super.onSaveInstanceState(outState);

  mNavigationManager.onSaveInstanceState(outState);
 
}
  @Override public void onBackPressed() {

  if (!mNavigationManager.closeDrawer()) {

super.onBackPressed();

  
}
 
}
5. To navigate with Intents:
Intent intent = NavigationManager.createNavigationIntent(R.id.menuId);
 intent.putExtra("argument", "dummy");
 mNavigationManager.navigateWithIntent(intent);

The intent will be automatically passed to your fragment

Optional Usage

1. Override createFragmentTransaction(Fragment fragment) to create your own FragmentTransactions

The default transaction is:

public FragmentTransaction createFragmentTransaction(Fragment fragment) {

  return mFragmentManager.beginTransaction()

 .replace(mContainerId, fragment, CURRENT_TITLE);
 
}
2. Override commitFragmentTransaction(FragmentTransaction)

The default commit is:

public void commitFragmentTransaction(FragmentTransaction transaction) {

  // We can allow state loss because the fragment will start for the first time
  transaction.commitAllowingStateLoss();
 
}
3. Override shouldDelayTransaction to enable/disable the delay between switching fragments

ActionMode pausing/resuming

When the NavigationView opens, you should finish or at least pause the ActionMode. To do this, you can implement NavigationManager.ActionModeListener in your fragments and then:

private ActionMode mActionMode; private boolean mActionModeSuspended = false;  @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

  // ...

 if (savedInstanceState != null) {

mActionModeSuspended

  = savedInstanceState.getBoolean(NavigationManager.ACTION_MODE_SUSPENDED);

 // Restore action mode state if it was active before

if (savedInstanceState.getBoolean(NavigationManager.ACTION_MODE_ACTIVE)) {

 mActionMode = ((AppCompatActivity) getActivity()).startSupportActionMode(this);

 // restore action mode data here

}

  
}

  return view; 
}
  @Override public void onSaveInstanceState(Bundle outState) {

  super.onSaveInstanceState(outState);

  // Save actionMode state. This will internally check if the ActionMode is active
  // or suspended by calling isActionModeActive or isActionModeSuspended
  NavigationManager.saveActionModeState(outState, this);
 
}
  @Override public void onSuspendActionMode() {

  if (mActionMode != null) {

// save ActionMode state here

mActionMode.finish();

mActionMode = null;

mActionModeSuspended = true;
  
}
 
}
  @Override public void onResumeActionMode() {

  mActionMode = ((AppCompatActivity) getActivity()).startSupportActionMode(this);

  mActionModeSuspended = false;
  // restore action mode state here 
}
  @Override public boolean isActionModeActive() {

  return mActionMode != null; 
}
  @Override public boolean isActionModeSuspended() {

  return mActionModeSuspended; 
}

Apps using NavigationManager (Send a PR to add your app here)

License

Copyright 2016 RĂºben Sousa  Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0  Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. 

Resources

OXO Flavored ImageViewZoom.

A logcat alike overlay to display log messages in your app as independent overlay.

Material Design EditText with a delightful linear ripple in background.

Vertically ViewPager and vertically transformer for Android.

An easy way to test performance, security and functionality on real devices.

Upload your .apk today and Monkop will test it for you. Get detailed report in your inbox in less than 24 hrs!

Convert OkHttp requests into curl logs.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes