Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Multiple turbolinks view with bottom navigation bar [Android]
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/home"
android:enabled="true"
android:icon="@drawable/ic_home_24"
android:title="@string/home"/>
<item
android:id="@+id/profile"
android:enabled="true"
android:icon="@drawable/ic_person_24"
android:title="@string/profile"/>
</menu>
/*
implementation 'androidx.viewpager2:viewpager2:1.1.0-alpha01'
implementation 'com.google.android.material:material:1.1.0-alpha08'
*/
public class MultipleTurbolinksViewActivity extends BaseActivity {
private ActivityMultipleTurbolinksViewBinding binding;
private String[] paths = new String[]{"/", "/profile"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMultipleTurbolinksViewBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
MultipleTurbolinksViewAdapter multipleTurbolinksViewAdapter =
new MultipleTurbolinksViewAdapter(
getSupportFragmentManager(),
getLifecycle(), paths);
binding.viewPager.setAdapter(multipleTurbolinksViewAdapter);
binding.viewPager.setUserInputEnabled(false); // To disable swipe
binding.bottomNavigation.setOnNavigationItemSelectedListener(item -> {
switch (item.getItemId()) {
case R.id.home:
binding.viewPager.setCurrentItem(0, true);
return true;
case R.id.profile:
binding.viewPager.setCurrentItem(1, true);
return true;
default:
return false;
}
});
}
@Override
protected void onRestart() {
super.onRestart();
refreshCurrentFragment();
}
@Override
protected void refresh() {
// Cold boot and refresh all fragments.
for (int i = 0; i < paths.length; i++) {
Fragment fragment = getSupportFragmentManager().findFragmentByTag("f" + i);
if(!(fragment instanceof TurbolinksViewFragment)) continue;
((TurbolinksViewFragment) fragment).resetToColdBoot();
((TurbolinksViewFragment) fragment).visitCurrentLocation(true);
}
}
private void refreshCurrentFragment() {
Fragment currentFragment = getSupportFragmentManager().findFragmentByTag("f" + binding.viewPager.getCurrentItem());
if(!(currentFragment instanceof TurbolinksViewFragment)) return;
((TurbolinksViewFragment) currentFragment).visitCurrentLocation(true);
}
}
public class MultipleTurbolinksViewAdapter extends FragmentStateAdapter {
private String[] paths;
public MultipleTurbolinksViewAdapter(@NonNull FragmentManager fragmentManager,
@NonNull Lifecycle lifecycle,
String[] paths) {
super(fragmentManager, lifecycle);
this.paths = paths;
}
@NonNull
@Override
public Fragment createFragment(int position) {
return TurbolinksViewFragment.newInstance(paths[position]);
}
@Override
public int getItemCount() {
return paths.length;
}
}
public class TurbolinksViewFragment extends Fragment {
private static final String PATH = "path";
private String path;
private FragmentTurbolinksViewBinding binding;
private TurbolinksSession customTurbolinksSession;
public TurbolinksViewFragment() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param path Path to visit.
* @return A new instance of fragment TurbolinksViewFragment.
*/
public static TurbolinksViewFragment newInstance(String path) {
TurbolinksViewFragment fragment = new TurbolinksViewFragment();
Bundle args = new Bundle();
args.putString(PATH, path);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
path = getArguments().getString(PATH);
}
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
binding = FragmentTurbolinksViewBinding.inflate(inflater, container, false);
Activity activity = getActivity();
if(activity == null) {
Log.d("NULL", "TurbolinksViewFragment - onCreateView - getActivity is null");
return null;
}
// Setup a new turbolinks session
customTurbolinksSession = TurbolinksSession.getNew(activity);
setupTurbolinksSession(customTurbolinksSession);
visitCurrentLocation(false);
return binding.getRoot();
}
public void resetToColdBoot() {
customTurbolinksSession.resetToColdBoot();
}
public void visitCurrentLocation(boolean restoreWithCachedSnapshot) {
Helper.turbolinksVisit(getActivity(), customTurbolinksSession,
binding.viewTurbolinks, Constants.BASE_URL + path, restoreWithCachedSnapshot);
}
/* Not required */
private void setupTurbolinksSession(TurbolinksSession turbolinksSession) {
turbolinksSession.setPullToRefreshEnabled(false);
turbolinksSession.getWebView().addJavascriptInterface(JsBridge.getInstance(), "AndroidBridge");
turbolinksSession.getWebView().setWebChromeClient(CustomChromeClient.getInstance());
turbolinksSession.getWebView().getSettings()
.setUserAgentString(Helper.getUserAgentString(getActivity()));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment