2 years ago

#63533

test-img

Sarthak Kumar

How can I open a dialog from another dialog?

My first dialog

enter image description here

My second dialog

enter image description here

I want to open second dialog when READ MORE is clicked in the first one and when I back press from the second dialog previous dialog should open. How can I achieve this?

I am using navigation architecture component and I tried using dialog fragment as a part of navigation graph but whenever I use findnavControlller() in my dialog fragment, I got an error while compiling. The error I was getting:-

Caused by: java.lang.IllegalStateException: Fragment StartDialog{25953bc} (e31ef9c7-7698-4ffa-af6d-fecc33fecd94) not associated with a fragment manager.

at androidx.fragment.app.Fragment.getParentFragmentManager(Fragment.java:1040)
at androidx.navigation.fragment.NavHostFragment.findNavController(NavHostFragment.java:109)
at androidx.navigation.fragment.FragmentKt.findNavController(Fragment.kt:29)
at com.example.thebookapp.main.streakTracker.streakTimer.StartDialog.<init>(StartDialog.kt:18)
at java.lang.reflect.Constructor.newInstance0(Native Method) 
at java.lang.reflect.Constructor.newInstance(Constructor.java:343) 
at androidx.fragment.app.Fragment.instantiate(Fragment.java:613) 
at androidx.fragment.app.FragmentContainer.instantiate(FragmentContainer.java:57) 
at androidx.fragment.app.FragmentManager$3.instantiate(FragmentManager.java:483) 
at androidx.navigation.fragment.DialogFragmentNavigator.navigate(DialogFragmentNavigator.java:115) 
at androidx.navigation.fragment.DialogFragmentNavigator.navigate(DialogFragmentNavigator.java:48) 
at androidx.navigation.NavController.navigate(NavController.java:1066) 
at androidx.navigation.NavController.navigate(NavController.java:944) 
at androidx.navigation.NavController.navigate(NavController.java:877) 
at androidx.navigation.NavController.navigate(NavController.java:863) 
at androidx.navigation.NavController.navigate(NavController.java:1159) 
at com.example.thebookapp.main.streakTracker.streakTimer.TimerFragment.onViewCreated$lambda-1(TimerFragment.kt:51) 
at com.example.thebookapp.main.streakTracker.streakTimer.TimerFragment.$r8$lambda$tVyB3ijdvhPSdchRqwOVwpBd4zU(Unknown Source:0) 
at com.example.thebookapp.main.streakTracker.streakTimer.TimerFragment$$ExternalSyntheticLambda1.onClick(Unknown Source:2) 

Edit 1:-

The screenshots I have posted and that I am going to post is not implemented using dialog fragment. It was implemented using simple dialog(), I was trying to add the backstack feature in dialogs, that's why I switched to dialog fragment but I was getting error.

First I was trying to implement the second dialog in which when a start button is clicked second dialog will be shown and when user select any option(5 minutes, 15 minutes), a timer will start.

Code for second dialog

package com.example.thebookapp.main.streakTracker.streakTimer

import android.os.Bundle
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import androidx.navigation.fragment.findNavController

import com.example.thebookapp.R
import com.google.android.material.bottomsheet.BottomSheetDialogFragment


class StartDialog: DialogFragment() {
    val navController = findNavController()
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        dialog?.setTitle("Duration")

        return inflater.inflate(R.layout.timer_dialog_layout, container, false)
    }

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

        view.findViewById<TextView>(R.id.td_10min).setOnClickListener {
            val action = StartDialogDirections.actionStartDialogToTimerFragment(10)
            navController.navigate(action)
        }
        view.findViewById<TextView>(R.id.td_20min).setOnClickListener {
            val action = StartDialogDirections.actionStartDialogToTimerFragment(20)
            navController.navigate(action)
        }
        view.findViewById<TextView>(R.id.td_30min).setOnClickListener {
            val action = StartDialogDirections.actionStartDialogToTimerFragment(30)
            navController.navigate(action)
        }
    }
}

My code for second dialog(start dialog) in nav graph:-

<dialog
    android:id="@+id/startDialog"
    android:name="com.example.thebookapp.main.streakTracker.streakTimer.StartDialog"
    android:label="StartDialog"
    tools:layout="@layout/timer_dialog_layout">
    <action
        android:id="@+id/action_startDialog_to_timerFragment"
        app:destination="@id/timerFragment"
        app:popUpTo="@id/timerFragment"
        app:popUpToInclusive="true"
        />
</dialog>

enter image description here

android

kotlin

android-dialogfragment

android-architecture-navigation

0 Answers

Your Answer

Accepted video resources