1

I have the following in my AndroidManifest.xml file:

<activity
    android:name=".PickemAllPicksResultsActivity"
    android:label="@string/title_activity_backActionBar"
    android:parentActivityName=".PoolDetailsActivity"
    android:screenOrientation="portrait" />

The above places a back button/arrow (in the action bar) in my child view in order to navigate back to the parent view.

I have the following layout for the child view:

<RelativeLayout
    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"
    tools:context=".PickemAllPicksResultsActivity" >

    <com.google.android.material.tabs.TabLayout
        android:id="@+id/tabLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <com.google.android.material.tabs.TabItem
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Weekly" />

        <com.google.android.material.tabs.TabItem
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Overall" />

    </com.google.android.material.tabs.TabLayout>

    <androidx.viewpager2.widget.ViewPager2
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tabLayout"
        android:layout_centerHorizontal="true" />

</RelativeLayout>

When i click on the back button in the action bar the app crashes because the parent view is executing the onCreate which is calling a Firebase function:

// Get the Pool Info
poolReference = mFirebaseDatabase.getReference("PICKEMPOOLS").child(poolID);

and poolID is null.

Question, why does this back button trigger the OnCreate function, but if I press the back button on the Android Navigation is just dismisses the screen and goes to the previous view?

Learn2Code
  • 1,974
  • 5
  • 24
  • 46
  • https://stackoverflow.com/a/56775054/5304075 this might help you to understand behavior of navigation component. – Arul Jul 23 '22 at 06:14
  • @Arul i guess my question is how can i `override` the `Back` button in my code, as it seems that by putting the Back button in the `AndroidManifest` i cannot control the behavior? I would think that if i could via an `override` function i can pass back the variables to the parent on the child dismisses. – Learn2Code Jul 23 '22 at 12:39

1 Answers1

0

From my understanding, if you want to get back click the event implemented below will give you a callback when back pressed.

// This callback will only be called when MyFragment is at least Started.
    OnBackPressedCallback callback = new OnBackPressedCallback(true /* enabled by default */) {
        @Override
        public void handleOnBackPressed() {
            // Handle the back button event
        }
    };
    requireActivity().getOnBackPressedDispatcher().addCallback(this, callback);

    // The callback can be enabled or disabled here or in handleOnBackPressed()
Arul
  • 1,031
  • 13
  • 23