Skip to content

Commit

Permalink
Merge pull request #102 from android/predictive_back_cross_fragment
Browse files Browse the repository at this point in the history
add cross-fragment predictive back animations using navigation component
  • Loading branch information
ashnohe authored Sep 22, 2023
2 parents 0c48487 + 81307dc commit 10bbcd1
Show file tree
Hide file tree
Showing 13 changed files with 215 additions and 75 deletions.
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ androidx-activity-compose = "androidx.activity:activity-compose:1.7.2"
androidx-navigation-fragment = { module = "androidx.navigation:navigation-fragment", version.ref = "androidx-navigation" }
androidx-navigation-compose = { module = "androidx.navigation:navigation-compose", version.ref = "androidx-navigation" }
androidx-navigation-testing = { module = "androidx.navigation:navigation-testing", version.ref = "androidx-navigation" }
androidx-navigation-ui = { module = "androidx.navigation:navigation-ui", version.ref = "androidx-navigation" }
androidx-lifecycle-viewmodel-compose = "androidx.lifecycle:lifecycle-viewmodel-compose:2.6.2"
androidx-viewpager2 = "androidx.viewpager2:viewpager2:1.0.0"

Expand Down
17 changes: 14 additions & 3 deletions samples/user-interface/predictiveback/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,19 @@

Shows different types of predictive back animations, including:

+ back-to-home
+ cross-activity
+ (coming soon) cross-fragment
+ Back-to-home
+ Cross-activity
+ Cross-fragment animations with navigation component default animations. Example code:

```xml
<action
android:id="..."
app:destination="..."
app:enterAnim="@animator/nav_default_enter_anim"
app:exitAnim="@animator/nav_default_exit_anim"
app:popEnterAnim="@animator/nav_default_pop_enter_anim"
app:popExitAnim="@animator/nav_default_pop_exit_anim" />
```

+ (coming soon) androidx-transitions
+ (coming soon) custom cross-activity
3 changes: 2 additions & 1 deletion samples/user-interface/predictiveback/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,6 @@ dependencies {
implementation(libs.androidx.appcompat)
implementation(libs.androidx.constraintlayout)
implementation(libs.mdc)
// Add samples specific dependencies
implementation(libs.androidx.navigation.fragment)
implementation(libs.androidx.navigation.ui)
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ val animations = mapOf<PBAnimation, PBAnimationText>(
),
PBAnimation.CROSS_FRAGMENT to PBAnimationText(
"Cross-Fragment",
"Animation example coming soon."
"Click to see cross-fragment animations with Navigation Component's default animators."
)
)
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,13 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.findNavController
import com.example.platform.ui.predictiveback.databinding.FragmentAnimationListBinding

class PBListFragment : Fragment() {

private var _binding: FragmentAnimationListBinding? = null
private val binding
get() = checkNotNull(_binding){
"Cannot access binding in PBListFragment because it is null."
}
private val binding get() = _binding!!
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?,
Expand All @@ -41,6 +39,14 @@ class PBListFragment : Fragment() {
return binding.root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

binding.crossFragmentCard.setOnClickListener {
findNavController().navigate(R.id.show_PBNavigationComponentDefaultAnimations)
}
}

override fun onDestroyView() {
super.onDestroyView()
_binding = null
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2023 The Android Open Source Project
*
* 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
*
* https://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.
*/

package com.example.platform.ui.predictiveback

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.example.platform.ui.predictiveback.databinding.FragmentNavigationComponentDefaultAnimationsBinding

class PBNavigationComponentDefaultAnimations : Fragment() {

private var _binding: FragmentNavigationComponentDefaultAnimationsBinding? = null
private val binding get() = _binding!!
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?,
): View {
_binding = FragmentNavigationComponentDefaultAnimationsBinding
.inflate(inflater, container, false)
return binding.root
}

override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="utf-8"?><!--
~ Copyright 2023 The Android Open Source Project
~
~ 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
~
~ https://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.
-->

<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<stroke android:width="2dp" />

<solid android:color="#3d84cd" />

<padding
android:left="1dp"
android:right="1dp"
android:bottom="1dp"
android:top="1dp" />

<corners android:radius="5dp" />
</shape>
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".PBListFragment">

<LinearLayout
android:layout_width="match_parent"
Expand All @@ -27,7 +29,6 @@
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="80dp"
android:layout_margin="16dp"
android:gravity="center"
android:text="@string/predictive_back_sample_first_activity_header"
Expand Down Expand Up @@ -134,9 +135,11 @@
</com.google.android.material.card.MaterialCardView>

<com.google.android.material.card.MaterialCardView
android:id="@+id/cross_fragment_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp">
android:layout_margin="16dp"
style="?attr/materialCardViewFilledStyle">

<LinearLayout
android:layout_width="match_parent"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?xml version="1.0" encoding="utf-8"?><!--
~ Copyright 2023 The Android Open Source Project
~
~ 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
~
~ https://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.
-->

<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"
android:layout_margin="16dp"
tools:context=".PBNavigationComponentDefaultAnimations">

<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/fragment_nav_component_header"
android:textAppearance="?attr/textAppearanceTitleMedium"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<View
android:id="@+id/box"
android:layout_width="300dp"
android:layout_height="300dp"
android:background="@drawable/box"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/body"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxWidth="300dp"
android:fontFamily="sans-serif"
android:text="@string/fragment_nav_component_body"
android:textAppearance="?attr/textAppearanceBodyMedium"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@+id/box"
app:layout_constraintStart_toStartOf="@+id/box"
app:layout_constraintTop_toBottomOf="@+id/box" />

</androidx.constraintlayout.widget.ConstraintLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@

<androidx.fragment.app.FragmentContainerView
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/fragment_container"
android:name="com.example.platform.ui.predictiveback.PBListFragment"
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/nav_graph"
tools:context=".PBHostingActivity" />

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?xml version="1.0" encoding="utf-8"?><!--
~ Copyright 2023 The Android Open Source Project
~
~ 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
~
~ https://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.
-->

<navigation 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/nav_graph"
app:startDestination="@id/PBListFragment">
<fragment
android:id="@+id/PBListFragment"
android:name="com.example.platform.ui.predictiveback.PBListFragment"
android:label="fragment_animation_list"
tools:layout="@layout/fragment_animation_list" >
<action
android:id="@+id/show_PBNavigationComponentDefaultAnimations"
app:destination="@id/PBNavigationComponentDefaultAnimations"
app:enterAnim="@animator/nav_default_enter_anim"
app:exitAnim="@animator/nav_default_exit_anim"
app:popEnterAnim="@animator/nav_default_pop_enter_anim"
app:popExitAnim="@animator/nav_default_pop_exit_anim" />
</fragment>
<fragment
android:id="@+id/PBNavigationComponentDefaultAnimations"
android:name="com.example.platform.ui.predictiveback.PBNavigationComponentDefaultAnimations"
android:label="fragment_navigation_component_default_animations"
tools:layout="@layout/fragment_navigation_component_default_animations" />
</navigation>
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@
<resources>
<string name="title_activity_predictive_back_sample">PredictiveBackSample</string>
<string name="predictive_back_sample_first_activity_header">Predictive Back Samples</string>
<string name="fragment_nav_component_header">Fragment</string>
<string name="fragment_nav_component_body">Swipe back to see Predictive Back cross-fragment animations using Navigation Component default animations.</string>
</resources>

0 comments on commit 10bbcd1

Please sign in to comment.