Created
December 3, 2019 12:21
-
-
Save yunusemredilber/718602d2eba92eeae86d793cd8e0e5ff to your computer and use it in GitHub Desktop.
Using Turbolinks Android with tabs.
This file contains 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
<?xml version="1.0" encoding="utf-8"?> | |
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
tools:context=".MainActivity"> | |
<FrameLayout | |
app:layout_constraintTop_toTopOf="parent" | |
app:layout_constraintLeft_toLeftOf="parent" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_marginBottom="55dp"> | |
<com.basecamp.turbolinks.TurbolinksView | |
android:id="@+id/tab1_turbolinks_view" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:layout_gravity="center" /> | |
<com.basecamp.turbolinks.TurbolinksView | |
android:id="@+id/tab2_turbolinks_view" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent"/> | |
</FrameLayout> | |
<com.google.android.material.bottomnavigation.BottomNavigationView | |
android:id="@+id/bottomNavigation" | |
android:layout_width="match_parent" | |
android:layout_height="55dp" | |
android:background="@color/bottomNavigationBackground" | |
app:itemIconTint="@color/bottomNavigationItem" | |
app:itemTextColor="@color/bottomNavigationItem" | |
android:layout_gravity="bottom" | |
app:layout_constraintBottom_toBottomOf="parent" | |
app:layout_constraintLeft_toLeftOf="parent" | |
app:menu="@menu/bottom_navigation" | |
app:labelVisibilityMode="unlabeled"/> | |
</androidx.constraintlayout.widget.ConstraintLayout> |
This file contains 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
public class MainActivity extends AppCompatActivity implements TurbolinksAdapter { | |
// Change the BASE_URL to an address that your VM or device can hit. | |
private static final String BASE_URL = "http://10.0.1.100:9292"; | |
private static final String INTENT_URL = "intentUrl"; | |
String tab1_path = "/tab1"; | |
String tab2_path = "/tab2"; | |
boolean tab1_is_visited = false; | |
boolean tab2_is_visited = false; | |
String selectedTab = ""; | |
TurbolinksView tab1TurbolinksView; | |
TurbolinksView tab2TurbolinksView; | |
TurbolinksSession tab1TurbolinksSession; | |
TurbolinksSession tab2TurbolinksSession; | |
BottomNavigationView bottomNavigationView; // Can be anything. I just used this. | |
// ----------------------------------------------------------------------- | |
// Activity overrides | |
// ----------------------------------------------------------------------- | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
// Find the custom TurbolinksView objects in layout. | |
tab1TurbolinksView = (TurbolinksView) findViewById(R.id.tab1_turbolinks_view); | |
tab2TurbolinksView = (TurbolinksView) findViewById(R.id.tab2_turbolinks_view); | |
// Create new TurbolinksSessions | |
tab1TurbolinksSession = TurbolinksSession.getNew(this); | |
tab2TurbolinksSession = TurbolinksSession.getNew(this); | |
// For this demo app, we force debug logging on. You will only want to do | |
// this for debug builds of your app (it is off by default) | |
TurbolinksSession.getDefault(this).setDebugLoggingEnabled(true); | |
// Dispatch a OnNavigationItemSelectedListener to bottomNavigationView for handling tab changes. | |
bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottomNavigation); | |
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() { | |
@Override | |
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) { | |
switch (menuItem.getItemId()) { | |
case R.id.tab1: | |
changeView("Tab1"); | |
break; | |
case R.id.tab2 : | |
changeView("Tab2"); | |
break; | |
} | |
return true; | |
} | |
}); | |
// Execute the visit for tab1 page. | |
visitTab1(false); | |
tab1_is_visited = true; | |
// Hide other webviews for preventing conflict. | |
tab2TurbolinksView.setVisibility(View.GONE); | |
} | |
@Override | |
protected void onRestart() { | |
super.onRestart(); | |
// Revisit active tabs webview. | |
if(selectedTab.equals("Tab1")) { | |
visitTab1(true); | |
} else if(selectedTab.equals("Tab2")) { | |
visitTab2(true); | |
} | |
} | |
// ----------------------------------------------------------------------- | |
// Private custom visit methods | |
// ----------------------------------------------------------------------- | |
private void visitTab1(Boolean restoreWithCachedSnapshot) { | |
tab1TurbolinksSession | |
.activity(this) | |
.adapter(this) | |
.restoreWithCachedSnapshot(restoreWithCachedSnapshot) | |
.view(tab1TurbolinksView) | |
.visit(BASE_URL + tab1_path); | |
} | |
private void visitTab2(Boolean restoreWithCachedSnapshot) { | |
tab2TurbolinksSession | |
.activity(this) | |
.adapter(this) | |
.restoreWithCachedSnapshot(restoreWithCachedSnapshot) | |
.view(tab2TurbolinksView) | |
.visit(BASE_URL + tab2_path); | |
} | |
@Override | |
public void visitProposedToLocationWithAction(String location, String action) { | |
// RegularActivity is a non-tabbed turbolinks activity for preventing possible errors. | |
Intent intent = new Intent(this, RegularActivity.class); | |
intent.putExtra(INTENT_URL, location); | |
this.startActivity(intent); | |
} | |
// ----------------------------------------------------------------------- | |
// Private methods | |
// ----------------------------------------------------------------------- | |
private void changeView(String name) { | |
if(name.equals("Tab1")) { | |
if(!tab1_is_visited) { | |
visitTab1(true); | |
tab1_is_visited = true; | |
} | |
tab1TurbolinksView.setVisibility(View.VISIBLE); | |
tab2TurbolinksView.setVisibility(View.GONE); | |
} else if(name.equals("Tab2")) { | |
if(!tab2_is_visited) { | |
visitTab2(true); | |
tab2_is_visited = true; | |
} | |
tab1TurbolinksView.setVisibility(View.GONE); | |
tab2TurbolinksView.setVisibility(View.VISIBLE); | |
} | |
selectedTab = name; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment