changed weird activity switching to smooth fragment switching
This commit is contained in:
parent
07be587796
commit
5a514a389a
7 changed files with 43 additions and 203 deletions
|
|
@ -16,17 +16,6 @@
|
||||||
<intent-filter>
|
<intent-filter>
|
||||||
<action android:name="android.intent.action.MAIN" />
|
<action android:name="android.intent.action.MAIN" />
|
||||||
|
|
||||||
<category android:name="android.intent.category.LAUNCHER" />
|
|
||||||
</intent-filter>
|
|
||||||
</activity>
|
|
||||||
<activity
|
|
||||||
android:name=".activities.DebugActivity"
|
|
||||||
android:exported="true"
|
|
||||||
android:theme="@style/Theme.ClimateGoApp.NoActionBar"
|
|
||||||
android:parentActivityName=".activities.HomeActivity">
|
|
||||||
<intent-filter>
|
|
||||||
<action android:name="android.intent.action.MAIN" />
|
|
||||||
|
|
||||||
<category android:name="android.intent.category.LAUNCHER" />
|
<category android:name="android.intent.category.LAUNCHER" />
|
||||||
</intent-filter>
|
</intent-filter>
|
||||||
</activity>
|
</activity>
|
||||||
|
|
|
||||||
|
|
@ -1,44 +0,0 @@
|
||||||
package de.cdaut.climategoapp.activities;
|
|
||||||
|
|
||||||
import android.content.Intent;
|
|
||||||
import android.view.MenuItem;
|
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
|
||||||
import androidx.appcompat.app.AppCompatActivity;
|
|
||||||
import androidx.drawerlayout.widget.DrawerLayout;
|
|
||||||
|
|
||||||
import de.cdaut.climategoapp.R;
|
|
||||||
|
|
||||||
public final class ActivityUtils {
|
|
||||||
private ActivityUtils() {
|
|
||||||
throw new IllegalAccessError("Utility Class");
|
|
||||||
}
|
|
||||||
|
|
||||||
public static boolean handleNavBarItemClicks(@NonNull MenuItem item,
|
|
||||||
AppCompatActivity rootClass,
|
|
||||||
DrawerLayout drawer) {
|
|
||||||
|
|
||||||
Class<? extends AppCompatActivity> targetActivityClass;
|
|
||||||
|
|
||||||
switch (item.getItemId()) {
|
|
||||||
case R.id.nav_main:
|
|
||||||
targetActivityClass = HomeActivity.class;
|
|
||||||
break;
|
|
||||||
case R.id.nav_debug:
|
|
||||||
targetActivityClass = DebugActivity.class;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
throw new IllegalAccessError("Illegal activity met in nav drawer");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (targetActivityClass.equals(rootClass.getClass())) {
|
|
||||||
drawer.closeDrawers();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
Intent targetIntent = new Intent(rootClass, targetActivityClass);
|
|
||||||
rootClass.startActivity(targetIntent);
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,103 +0,0 @@
|
||||||
package de.cdaut.climategoapp.activities;
|
|
||||||
|
|
||||||
import android.content.Intent;
|
|
||||||
import android.os.Bundle;
|
|
||||||
|
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
|
||||||
import androidx.appcompat.app.ActionBarDrawerToggle;
|
|
||||||
import androidx.appcompat.app.AppCompatActivity;
|
|
||||||
|
|
||||||
import androidx.drawerlayout.widget.DrawerLayout;
|
|
||||||
import androidx.navigation.NavController;
|
|
||||||
import androidx.navigation.Navigation;
|
|
||||||
import androidx.navigation.ui.AppBarConfiguration;
|
|
||||||
import androidx.navigation.ui.NavigationUI;
|
|
||||||
|
|
||||||
import de.cdaut.climategoapp.R;
|
|
||||||
import de.cdaut.climategoapp.databinding.ActivityDebugBinding;
|
|
||||||
|
|
||||||
import android.view.Menu;
|
|
||||||
import android.view.MenuItem;
|
|
||||||
|
|
||||||
import com.google.android.material.navigation.NavigationView;
|
|
||||||
|
|
||||||
public class DebugActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
|
|
||||||
|
|
||||||
private AppBarConfiguration appBarConfiguration;
|
|
||||||
private ActivityDebugBinding binding;
|
|
||||||
public DrawerLayout drawerLayout;
|
|
||||||
public ActionBarDrawerToggle actionBarDrawerToggle;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
|
||||||
super.onCreate(savedInstanceState);
|
|
||||||
|
|
||||||
binding = ActivityDebugBinding.inflate(getLayoutInflater());
|
|
||||||
setContentView(binding.getRoot());
|
|
||||||
|
|
||||||
setSupportActionBar(binding.toolbar);
|
|
||||||
|
|
||||||
|
|
||||||
// drawer layout instance to toggle the menu icon to open
|
|
||||||
// drawer and back button to close drawer
|
|
||||||
drawerLayout = findViewById(R.id.debug_drawer_layout);
|
|
||||||
actionBarDrawerToggle = new ActionBarDrawerToggle(this,
|
|
||||||
drawerLayout,
|
|
||||||
R.string.nav_open,
|
|
||||||
R.string.nav_close);
|
|
||||||
|
|
||||||
// pass the Open and Close toggle for the drawer layout listener
|
|
||||||
// to toggle the button
|
|
||||||
drawerLayout.addDrawerListener(actionBarDrawerToggle);
|
|
||||||
actionBarDrawerToggle.syncState();
|
|
||||||
|
|
||||||
// to make the Navigation drawer icon always appear on the action bar
|
|
||||||
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
|
|
||||||
|
|
||||||
setNavigationViewListener();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean onCreateOptionsMenu(Menu menu) {
|
|
||||||
// Inflate the menu; this adds items to the action bar if it is present.
|
|
||||||
getMenuInflater().inflate(R.menu.menu_main, menu);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean onOptionsItemSelected(MenuItem item) {
|
|
||||||
// Handle action bar item clicks here. The action bar will
|
|
||||||
// automatically handle clicks on the Home/Up button, so long
|
|
||||||
// as you specify a parent activity in AndroidManifest.xml.
|
|
||||||
int id = item.getItemId();
|
|
||||||
|
|
||||||
if (actionBarDrawerToggle.onOptionsItemSelected(item)) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
//noinspection SimplifiableIfStatement
|
|
||||||
if (id == R.id.action_settings) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return super.onOptionsItemSelected(item);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean onSupportNavigateUp() {
|
|
||||||
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_nav);
|
|
||||||
return NavigationUI.navigateUp(navController, appBarConfiguration)
|
|
||||||
|| super.onSupportNavigateUp();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
|
|
||||||
return ActivityUtils.handleNavBarItemClicks(item, this, drawerLayout);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void setNavigationViewListener() {
|
|
||||||
NavigationView navigationView = findViewById(R.id.nav_drawer);
|
|
||||||
navigationView.setNavigationItemSelectedListener(this);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,6 +1,5 @@
|
||||||
package de.cdaut.climategoapp.activities;
|
package de.cdaut.climategoapp.activities;
|
||||||
|
|
||||||
import android.content.Intent;
|
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -9,11 +8,14 @@ import androidx.appcompat.app.ActionBarDrawerToggle;
|
||||||
import androidx.appcompat.app.AppCompatActivity;
|
import androidx.appcompat.app.AppCompatActivity;
|
||||||
|
|
||||||
import androidx.drawerlayout.widget.DrawerLayout;
|
import androidx.drawerlayout.widget.DrawerLayout;
|
||||||
|
import androidx.fragment.app.Fragment;
|
||||||
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.AppBarConfiguration;
|
||||||
import androidx.navigation.ui.NavigationUI;
|
import androidx.navigation.ui.NavigationUI;
|
||||||
|
|
||||||
|
import de.cdaut.climategoapp.DebugFragment;
|
||||||
|
import de.cdaut.climategoapp.HomeFragment;
|
||||||
import de.cdaut.climategoapp.R;
|
import de.cdaut.climategoapp.R;
|
||||||
import de.cdaut.climategoapp.databinding.ActivityHomeBinding;
|
import de.cdaut.climategoapp.databinding.ActivityHomeBinding;
|
||||||
|
|
||||||
|
|
@ -52,7 +54,13 @@ public class HomeActivity extends AppCompatActivity implements NavigationView.On
|
||||||
actionBarDrawerToggle.syncState();
|
actionBarDrawerToggle.syncState();
|
||||||
|
|
||||||
// to make the Navigation drawer icon always appear on the action bar
|
// to make the Navigation drawer icon always appear on the action bar
|
||||||
getSupportActionBar(). setDisplayHomeAsUpEnabled(true);
|
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
|
||||||
|
|
||||||
|
//add the Home Fragment to the Fragment container
|
||||||
|
this.getSupportFragmentManager()
|
||||||
|
.beginTransaction()
|
||||||
|
.replace(R.id.home_act_fragment_container, new HomeFragment())
|
||||||
|
.commit();
|
||||||
|
|
||||||
setNavigationViewListener();
|
setNavigationViewListener();
|
||||||
}
|
}
|
||||||
|
|
@ -93,7 +101,30 @@ public class HomeActivity extends AppCompatActivity implements NavigationView.On
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
|
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
|
||||||
return ActivityUtils.handleNavBarItemClicks(item, this, drawerLayout);
|
|
||||||
|
Fragment targetFragment;
|
||||||
|
|
||||||
|
//determine which fragment to switch to
|
||||||
|
switch (item.getItemId()) {
|
||||||
|
case R.id.nav_main:
|
||||||
|
targetFragment = new HomeFragment();
|
||||||
|
break;
|
||||||
|
case R.id.nav_debug:
|
||||||
|
targetFragment = new DebugFragment();
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw new IllegalAccessError("Illegal activity met in nav drawer");
|
||||||
|
}
|
||||||
|
|
||||||
|
//switch to the appropriate Fragment
|
||||||
|
this.getSupportFragmentManager()
|
||||||
|
.beginTransaction()
|
||||||
|
.replace(R.id.home_act_fragment_container, targetFragment)
|
||||||
|
.commit();
|
||||||
|
|
||||||
|
//close the nav drawer after switching fragments
|
||||||
|
this.drawerLayout.closeDrawers();
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setNavigationViewListener() {
|
private void setNavigationViewListener() {
|
||||||
|
|
|
||||||
|
|
@ -1,37 +0,0 @@
|
||||||
<?xml version="1.0" encoding="utf-8"?>
|
|
||||||
<androidx.drawerlayout.widget.DrawerLayout 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:id="@+id/debug_drawer_layout"
|
|
||||||
android:layout_width="match_parent"
|
|
||||||
android:layout_height="match_parent"
|
|
||||||
tools:context=".activities.HomeActivity"
|
|
||||||
tools:ignore="HardcodedText">
|
|
||||||
|
|
||||||
<LinearLayout
|
|
||||||
android:layout_width="match_parent"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
android:orientation="vertical"
|
|
||||||
android:theme="@style/Theme.ClimateGoApp.AppBarOverlay">
|
|
||||||
|
|
||||||
<androidx.appcompat.widget.Toolbar
|
|
||||||
android:id="@+id/toolbar"
|
|
||||||
android:layout_width="match_parent"
|
|
||||||
android:layout_height="?attr/actionBarSize"
|
|
||||||
android:background="?attr/colorPrimary"
|
|
||||||
app:popupTheme="@style/Theme.ClimateGoApp.PopupOverlay" />
|
|
||||||
|
|
||||||
<include layout="@layout/fragment_debug" />
|
|
||||||
</LinearLayout>
|
|
||||||
|
|
||||||
<!--this the navigation view which draws
|
|
||||||
and shows the navigation drawer-->
|
|
||||||
<!--include the menu created in the menu folder-->
|
|
||||||
<com.google.android.material.navigation.NavigationView
|
|
||||||
android:id="@+id/nav_drawer"
|
|
||||||
android:layout_width="wrap_content"
|
|
||||||
android:layout_height="match_parent"
|
|
||||||
android:layout_gravity="start"
|
|
||||||
app:menu="@menu/navigation_menu" />
|
|
||||||
|
|
||||||
</androidx.drawerlayout.widget.DrawerLayout>
|
|
||||||
|
|
@ -21,7 +21,11 @@
|
||||||
android:background="?attr/colorPrimary"
|
android:background="?attr/colorPrimary"
|
||||||
app:popupTheme="@style/Theme.ClimateGoApp.PopupOverlay" />
|
app:popupTheme="@style/Theme.ClimateGoApp.PopupOverlay" />
|
||||||
|
|
||||||
<include layout="@layout/fragment_home" />
|
<FrameLayout
|
||||||
|
android:id="@+id/home_act_fragment_container"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent">
|
||||||
|
</FrameLayout>
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
|
|
||||||
<!--this the navigation view which draws
|
<!--this the navigation view which draws
|
||||||
|
|
|
||||||
|
|
@ -3,21 +3,21 @@
|
||||||
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/HomeFragment">
|
app:startDestination="@id/home_fragment">
|
||||||
|
|
||||||
<fragment
|
<fragment
|
||||||
android:id="@+id/HomeFragment"
|
android:id="@+id/home_fragment"
|
||||||
android:name="de.cdaut.climategoapp.HomeFragment"
|
android:name="de.cdaut.climategoapp.HomeFragment"
|
||||||
android:label="@string/first_fragment_label"
|
android:label="@string/first_fragment_label"
|
||||||
tools:layout="@layout/fragment_home"/>
|
tools:layout="@layout/fragment_home"/>
|
||||||
|
|
||||||
<fragment android:id="@+id/DebugFragment"
|
<fragment android:id="@+id/debug_fragment"
|
||||||
android:name="de.cdaut.climategoapp.DebugFragment"
|
android:name="de.cdaut.climategoapp.DebugFragment"
|
||||||
android:label="@string/debug_fragment_label"
|
android:label="@string/debug_fragment_label"
|
||||||
tools:layout="@layout/fragment_debug">
|
tools:layout="@layout/fragment_debug">
|
||||||
<action
|
<action
|
||||||
android:id="@+id/action_DebugFragment_to_HomeFragment"
|
android:id="@+id/action_DebugFragment_to_HomeFragment"
|
||||||
app:destination="@id/HomeFragment" />
|
app:destination="@id/home_fragment" />
|
||||||
</fragment>
|
</fragment>
|
||||||
|
|
||||||
</navigation>
|
</navigation>
|
||||||
Loading…
Add table
Add a link
Reference in a new issue