Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/mb-rum-fragments' into mb-rum-fr…
Browse files Browse the repository at this point in the history
…agments
  • Loading branch information
TheRealAgentK committed Jan 17, 2025
2 parents ec8954f + b85796d commit 312fcf6
Show file tree
Hide file tree
Showing 12 changed files with 233 additions and 19 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ dependencies {
implementation(libs.androidx.constraintlayout.constraintlayout)
implementation(libs.android.material.material)
implementation(libs.androidx.multidex.multidex)
implementation libs.androidx.activity
implementation(libs.androidx.activity)

testImplementation(libs.junit)
testImplementation(libs.mockito.core)
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
<activity
android:name=".fragments.NavigationActivity"
android:parentActivityName=".MainActivity" />

<activity
android:name=".fragments.NavigationFragmentSwapActivity"
android:parentActivityName=".MainActivity" />
<meta-data
android:name="com.raygun.raygun4android.apikey"
android:value="YOUR_API_KEY_HERE" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import com.google.android.material.snackbar.Snackbar
import com.raygun.raygun4android.RaygunClient
import com.raygun.raygun4android.messages.shared.RaygunUserInfo
import com.raygun.raygun4android.sample.fragments.NavigationActivity
import com.raygun.raygun4android.sample.fragments.NavigationFragmentSwapActivity

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
Expand Down Expand Up @@ -41,6 +42,7 @@ class MainActivity : AppCompatActivity() {
val textViewProviderVersion = findViewById<TextView>(R.id.textView_providerVersion)
val buttonSecondActivity = findViewById<Button>(R.id.button_secondActivity)
val buttonNavigationActivity = findViewById<Button>(R.id.button_navigationActivity)
val buttonFragmentSwapActivity = findViewById<Button>(R.id.button_navigationFragmentSwapActivity)

buttonSend.setOnClickListener {
val tw = HashMap<String, String>()
Expand Down Expand Up @@ -149,6 +151,11 @@ class MainActivity : AppCompatActivity() {
this.startActivity(NavigationActivity.getIntent(this))
}

buttonFragmentSwapActivity.setOnClickListener {
// Launches an Activity containing a Fragment
this.startActivity(NavigationFragmentSwapActivity.getIntent(this))
}

textViewAppVersion.text =
getString(
R.string.app_version_text,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.raygun.raygun4android.sample.fragments

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import androidx.fragment.app.Fragment
import com.raygun.raygun4android.sample.R

class FragmentSwap1 : Fragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?,
): View? {
val view = inflater.inflate(R.layout.fragment_swap1, container, false)
view.findViewById<Button>(R.id.button).setOnClickListener {
parentFragmentManager.beginTransaction().apply {
replace(R.id.fragment_container_view, FragmentSwap2())
commit()
}
}
return view
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.raygun.raygun4android.sample.fragments

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import androidx.fragment.app.Fragment
import com.raygun.raygun4android.sample.R

class FragmentSwap2 : Fragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?,
): View? {
val view = inflater.inflate(R.layout.fragment_swap2, container, false)
view.findViewById<Button>(R.id.button).setOnClickListener {
parentFragmentManager.beginTransaction().apply {
replace(R.id.fragment_container_view, FragmentSwap1())
commit()
}
}
return view
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.raygun.raygun4android.sample.fragments

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.raygun.raygun4android.sample.R

class NavigationFragmentSwapActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContentView(R.layout.activity_navigation_fragment_swap)
setTitle(R.string.fragment_swap_text)
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
insets
}
}

companion object {
fun getIntent(context: Context): Intent = Intent(context, NavigationFragmentSwapActivity::class.java)
}
}
11 changes: 11 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,15 @@
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button_secondActivity" />

<Button
android:id="@+id/button_navigationFragmentSwapActivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="@string/go_to_a_fragment_swap_activity"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button_navigationActivity" />

</androidx.constraintlayout.widget.ConstraintLayout>
20 changes: 20 additions & 0 deletions app/src/main/res/layout/activity_navigation_fragment_swap.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragments.NavigationFragmentSwapActivity">

<androidx.fragment.app.FragmentContainerView
android:id="@+id/fragment_container_view"
android:name="com.raygun.raygun4android.sample.fragments.FragmentSwap1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
30 changes: 30 additions & 0 deletions app/src/main/res/layout/fragment_swap1.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?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=".fragments.FragmentSwap1">

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:text="@string/fragmentswap1_title"
android:textAppearance="@style/TextAppearance.AppCompat.Display1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:text="@string/go_to_fragmentswap2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2" />

</androidx.constraintlayout.widget.ConstraintLayout>
30 changes: 30 additions & 0 deletions app/src/main/res/layout/fragment_swap2.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?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=".fragments.FragmentSwap2">

<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:text="@string/fragmentswap2_title"
android:textAppearance="@style/TextAppearance.AppCompat.Display1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:text="@string/go_to_fragmentswap1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView3" />

</androidx.constraintlayout.widget.ConstraintLayout>
6 changes: 6 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,10 @@
<string name="we_returned_to_first_activity">We\'re now back from second activity.</string>
<string name="fragment_text">Fragment Screen</string>
<string name="go_to_a_navigation_activity">Go to a navigation Activity</string>
<string name="fragmentswap1_title">FragmentSwap1</string>
<string name="go_to_fragmentswap2">Go to FragmentSwap2</string>
<string name="fragmentswap2_title">FragmentSwap2</string>
<string name="go_to_fragmentswap1">Go to FragmentSwap1</string>
<string name="fragment_swap_text">Fragment Swap Screen</string>
<string name="go_to_a_fragment_swap_activity">Go to a Fragment swap Activity</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -42,32 +42,44 @@ public void onFragmentCreated(
@NonNull Fragment fragment,
@Nullable Bundle savedInstanceState) {
super.onFragmentCreated(fm, fragment, savedInstanceState);
RaygunLogger.v("RUM - Fragment created: " + getFragmentName(fragment));
if (!fragmentStartTime.containsKey(fragment.getId())) {
fragmentStartTime.put(fragment.getId(), System.nanoTime());
RaygunLogger.v(
"RUM - Fragment created: "
+ getFragmentName(fragment)
+ " id: "
+ getUniqueId(fragment));
if (!fragmentStartTime.containsKey(getUniqueId(fragment))) {
fragmentStartTime.put(getUniqueId(fragment), System.nanoTime());
}
rum.seen();
}

@Override
public void onFragmentStarted(@NonNull FragmentManager fm, @NonNull Fragment fragment) {
super.onFragmentStarted(fm, fragment);
RaygunLogger.v("RUM - Fragment started: " + getFragmentName(fragment));
if (!fragmentStartTime.containsKey(fragment.getId())) {
fragmentStartTime.put(fragment.getId(), System.nanoTime());
RaygunLogger.v(
"RUM - Fragment started: "
+ getFragmentName(fragment)
+ " id: "
+ getUniqueId(fragment));
if (!fragmentStartTime.containsKey(getUniqueId(fragment))) {
fragmentStartTime.put(getUniqueId(fragment), System.nanoTime());
}
rum.seen();
}

@Override
public void onFragmentResumed(@NonNull FragmentManager fm, @NonNull Fragment fragment) {
super.onFragmentResumed(fm, fragment);
RaygunLogger.v("RUM - Fragment resumed: " + getFragmentName(fragment));
RaygunLogger.v(
"RUM - Fragment resumed: "
+ getFragmentName(fragment)
+ " id: "
+ getUniqueId(fragment));
long duration = 0;
if (!fragmentStartTime.containsKey(fragment.getId())) {
fragmentStartTime.put(fragment.getId(), System.nanoTime());
if (!fragmentStartTime.containsKey(getUniqueId(fragment))) {
fragmentStartTime.put(getUniqueId(fragment), System.nanoTime());
} else {
Long startTime = fragmentStartTime.get(fragment.getId());
Long startTime = fragmentStartTime.get(getUniqueId(fragment));
if (startTime != null) {
long diff = System.nanoTime() - startTime;
duration = TimeUnit.NANOSECONDS.toMillis(diff);
Expand All @@ -79,27 +91,39 @@ public void onFragmentResumed(@NonNull FragmentManager fm, @NonNull Fragment fra
}

@Override
public void onFragmentPaused(@NonNull FragmentManager fm, @NonNull Fragment f) {
super.onFragmentPaused(fm, f);
RaygunLogger.v(" RUM - Fragment paused: " + getFragmentName(f));
public void onFragmentPaused(@NonNull FragmentManager fm, @NonNull Fragment fragment) {
super.onFragmentPaused(fm, fragment);
RaygunLogger.v(
"RUM - Fragment paused: "
+ getFragmentName(fragment)
+ " id: "
+ getUniqueId(fragment));
rum.seen();
}

@Override
public void onFragmentStopped(@NonNull FragmentManager fm, @NonNull Fragment fragment) {
super.onFragmentStopped(fm, fragment);
RaygunLogger.v("RUM - Fragment stopped: " + getFragmentName(fragment));
RaygunLogger.v(
"RUM - Fragment stopped: "
+ getFragmentName(fragment)
+ " id: "
+ getUniqueId(fragment));
// Remove the start time for the fragment
fragmentStartTime.remove(fragment.getId());
fragmentStartTime.remove(getUniqueId(fragment));
rum.seen();
}

@Override
public void onFragmentDestroyed(@NonNull FragmentManager fm, @NonNull Fragment fragment) {
super.onFragmentDestroyed(fm, fragment);
RaygunLogger.v("RUM - Fragment destroyed: " + getFragmentName(fragment));
RaygunLogger.v(
"RUM - Fragment destroyed: "
+ getFragmentName(fragment)
+ " id: "
+ getUniqueId(fragment));
// Remove the start time for the fragment
fragmentStartTime.remove(fragment.getId());
fragmentStartTime.remove(getUniqueId(fragment));
rum.seen();
}

Expand All @@ -112,4 +136,8 @@ public String getFragmentName(Fragment fragment) {
return simpleName;
}
}

public int getUniqueId(Fragment fragment) {
return fragment.hashCode();
}
}

0 comments on commit 312fcf6

Please sign in to comment.