Navigation refactoring

This commit is contained in:
Daniele Verducci (Slimpenguin) 2022-01-31 08:35:57 +01:00
parent 8dc6da8b98
commit 63b08fe718
2 changed files with 14 additions and 15 deletions

View File

@ -2,23 +2,16 @@ package it.danieleverducci.ojo.ui;
import android.os.Bundle; import android.os.Bundle;
import com.google.android.material.snackbar.Snackbar;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import android.view.View; import android.view.View;
import androidx.navigation.NavController; import androidx.navigation.NavController;
import androidx.navigation.Navigation; import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import it.danieleverducci.ojo.R; import it.danieleverducci.ojo.R;
import it.danieleverducci.ojo.databinding.ActivityMainBinding; import it.danieleverducci.ojo.databinding.ActivityMainBinding;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity { public class MainActivity extends AppCompatActivity {
private ActivityMainBinding binding; private ActivityMainBinding binding;
@ -33,7 +26,7 @@ public class MainActivity extends AppCompatActivity {
// Show FAB only on first fragment // Show FAB only on first fragment
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main); NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);
navController.addOnDestinationChangedListener((controller, destination, arguments) -> { navController.addOnDestinationChangedListener((controller, destination, arguments) -> {
if (destination.getId() == R.id.FirstFragment) if (destination.getId() == R.id.HomeFragment)
binding.fab.show(); binding.fab.show();
else else
binding.fab.hide(); binding.fab.hide();
@ -42,7 +35,7 @@ public class MainActivity extends AppCompatActivity {
binding.fab.setOnClickListener(new View.OnClickListener() { binding.fab.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View view) { public void onClick(View view) {
navController.navigate(R.id.action_FirstFragment_to_SecondFragment); navController.navigate(R.id.action_homeToSettings);
} }
}); });
} }

View File

@ -3,23 +3,26 @@
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/nav_graph" android:id="@+id/nav_graph"
app:startDestination="@id/FirstFragment"> app:startDestination="@id/HomeFragment">
<fragment <fragment
android:id="@+id/FirstFragment" android:id="@+id/HomeFragment"
android:name="it.danieleverducci.ojo.ui.SurveillanceFragment" android:name="it.danieleverducci.ojo.ui.SurveillanceFragment"
android:label="@string/first_fragment_label" android:label="@string/first_fragment_label"
tools:layout="@layout/fragment_surveillance"> tools:layout="@layout/fragment_surveillance">
<action <action
android:id="@+id/action_FirstFragment_to_SecondFragment" android:id="@+id/action_homeToSettings"
app:destination="@id/SettingsFragment" /> app:destination="@id/SettingsFragment" />
</fragment> </fragment>
<fragment <fragment
android:id="@+id/SecondFragment" android:id="@+id/CameraUrlFragment"
android:name="it.danieleverducci.ojo.ui.AddStreamFragment" android:name="it.danieleverducci.ojo.ui.AddStreamFragment"
android:label="@string/second_fragment_label" android:label="@string/second_fragment_label"
tools:layout="@layout/fragment_add_stream"> tools:layout="@layout/fragment_add_stream">
<action
android:id="@+id/action_cameraUrlToSettings"
app:destination="@id/SettingsFragment" />
</fragment> </fragment>
<fragment <fragment
android:id="@+id/SettingsFragment" android:id="@+id/SettingsFragment"
@ -28,7 +31,10 @@
tools:layout="@layout/fragment_settings_item_list" > tools:layout="@layout/fragment_settings_item_list" >
<action <action
android:id="@+id/action_SecondFragment_to_FirstFragment" android:id="@+id/action_settingsToHome"
app:destination="@id/FirstFragment" /> app:destination="@id/HomeFragment" />
<action
android:id="@+id/action_settingsToCameraUrl"
app:destination="@id/CameraUrlFragment" />
</fragment> </fragment>
</navigation> </navigation>