Preview:
ActivityMain.kt: 
package com.example.quizapp 
 
import androidx.appcompat.app.AppCompatActivity 
import android.os.Bundle 
 
class MainActivity : AppCompatActivity() { 
    override fun onCreate(savedInstanceState: Bundle?) { 
        super.onCreate(savedInstanceState) 
        setContentView(R.layout.activity_main) 
 
    } 
    companion object{ 
        var count:Int=0 
    } 
} 
main_activity: 
<?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=".MainActivity"> 
 
    <androidx.fragment.app.FragmentContainerView 
        android:id="@+id/fragmentContainerView" 
        android:name="androidx.navigation.fragment.NavHostFragment" 
        android:layout_width="409dp" 
        android:layout_height="729dp" 
        app:defaultNavHost="true" 
        app:layout_constraintBottom_toBottomOf="parent" 
        app:layout_constraintEnd_toEndOf="parent" 
        app:layout_constraintStart_toStartOf="parent" 
        app:layout_constraintTop_toTopOf="parent" 
        app:navGraph="@navigation/navgraph" /> 
</androidx.constraintlayout.widget.ConstraintLayout> 
win.kt: 
package com.example.quizapp 
 
import android.os.Bundle 
import androidx.fragment.app.Fragment 
import android.view.LayoutInflater 
import android.view.View 
import android.view.ViewGroup 
import android.widget.Button 
import android.widget.TextView 
import androidx.navigation.Navigation 
 
private const val ARG_PARAM1 = "param1" 
private const val ARG_PARAM2 = "param2" 
 
class win : Fragment() { 
    // TODO: Rename and change types of parameters 
    private var param1: String? = null 
    private var param2: String? = null 
 
    override fun onCreate(savedInstanceState: Bundle?) { 
        super.onCreate(savedInstanceState) 
        arguments?.let { 
            param1 = it.getString(ARG_PARAM1) 
            param2 = it.getString(ARG_PARAM2) 
        } 
    } 
 
    override fun onCreateView( 
        inflater: LayoutInflater, container: ViewGroup?, 
        savedInstanceState: Bundle? 
    ): View? { 
        var view:View=inflater.inflate(R.layout.fragment_win, container, false) 
        var tv:TextView=view.findViewById(R.id.textView) 
        var btn:Button=view.findViewById(R.id.next) 
        if(MainActivity.count==4) 
            tv.text="Congratulations you won the quiz \n score : "+MainActivity.count 
        else 
            tv.text="" 
        btn.setOnClickListener{ 
            tv.text="" 
            MainActivity.count=0 
            Navigation.findNavController(view).navigate(R.id.action_win2_to_quest1) 
        } 
        return view 
    } 
 
    companion object { 
                @JvmStatic 
        fun newInstance(param1: String, param2: String) = 
            win().apply { 
                arguments = Bundle().apply { 
                    putString(ARG_PARAM1, param1) 
                    putString(ARG_PARAM2, param2) 
                } 
            } 
    } 
} 
fragment_win.xml: 
<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout 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=".win"> 
 
    <androidx.constraintlayout.widget.ConstraintLayout 
        android:layout_width="match_parent" 
        android:layout_height="match_parent"> 
 
        <Button 
            android:id="@+id/next" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:text="Start quiz" 
            app:layout_constraintBottom_toBottomOf="parent" 
            app:layout_constraintEnd_toEndOf="parent" 
            app:layout_constraintStart_toStartOf="parent" 
            app:layout_constraintTop_toTopOf="parent" 
            app:layout_constraintVertical_bias="0.314" /> 
 
        <TextView 
            android:id="@+id/textView" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:textColor="#000000" 
            android:textSize="28dp" 
            app:layout_constraintBottom_toBottomOf="parent" 
            app:layout_constraintEnd_toEndOf="parent" 
            app:layout_constraintHorizontal_bias="0.498" 
            app:layout_constraintStart_toStartOf="parent" 
            app:layout_constraintTop_toTopOf="parent" 
            app:layout_constraintVertical_bias="0.593" /> 
 
    </androidx.constraintlayout.widget.ConstraintLayout> 
 
</FrameLayout> 
lose.kt: 
package com.example.quizapp 
 
import android.os.Bundle 
import androidx.fragment.app.Fragment 
import android.view.LayoutInflater 
import android.view.View 
import android.view.ViewGroup 
import android.widget.Button 
import androidx.navigation.Navigation 
 
// TODO: Rename parameter arguments, choose names that match 
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER 
private const val ARG_PARAM1 = "param1" 
private const val ARG_PARAM2 = "param2" 
 
class lose : Fragment() { 
    // TODO: Rename and change types of parameters 
    private var param1: String? = null 
    private var param2: String? = null 
 
    override fun onCreate(savedInstanceState: Bundle?) { 
        super.onCreate(savedInstanceState) 
        arguments?.let { 
            param1 = it.getString(ARG_PARAM1) 
            param2 = it.getString(ARG_PARAM2) 
        } 
    } 
 
    override fun onCreateView( 
        inflater: LayoutInflater, container: ViewGroup?, 
        savedInstanceState: Bundle? 
    ): View? { 
 
        var view:View= inflater.inflate(R.layout.fragment_lose, container, false) 
        var btn:Button=view.findViewById(R.id.next) 
        btn.setOnClickListener { 
            Navigation.findNavController(view).navigate(R.id.action_lose_to_win2) 
 
        } 
        return view 
    } 
 
    companion object { 
        @JvmStatic 
        fun newInstance(param1: String, param2: String) = 
            lose().apply { 
                arguments = Bundle().apply { 
                    putString(ARG_PARAM1, param1) 
                    putString(ARG_PARAM2, param2) 
                } 
            } 
    } 
} 
fragment_lose.xml: 
<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout 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=".lose"> 
 
    <androidx.constraintlayout.widget.ConstraintLayout 
        android:layout_width="match_parent" 
        android:layout_height="match_parent"> 
 
        <Button 
            android:id="@+id/next" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:text="Try again" 
            app:layout_constraintBottom_toBottomOf="parent" 
            app:layout_constraintEnd_toEndOf="parent" 
            app:layout_constraintHorizontal_bias="0.498" 
            app:layout_constraintStart_toStartOf="parent" 
            app:layout_constraintTop_toBottomOf="@+id/textView" 
            app:layout_constraintVertical_bias="0.5" /> 
 
        <TextView 
            android:id="@+id/textView" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:text="Sorry you lost the quiz" 
            android:textColor="#000000" 
            android:textSize="28dp" 
            app:layout_constraintBottom_toBottomOf="parent" 
            app:layout_constraintEnd_toEndOf="parent" 
            app:layout_constraintStart_toStartOf="parent" 
            app:layout_constraintTop_toTopOf="parent" 
            app:layout_constraintVertical_bias="0.357" /> 
 
    </androidx.constraintlayout.widget.ConstraintLayout> 
</FrameLayout> 
quest1.kt: 
package com.example.quizapp 
 
import android.os.Bundle 
import android.provider.MediaStore.Audio.Radio 
import androidx.fragment.app.Fragment 
import android.view.LayoutInflater 
import android.view.View 
import android.view.ViewGroup 
import android.widget.Button 
import android.widget.RadioGroup 
import androidx.lifecycle.findViewTreeViewModelStoreOwner 
import androidx.navigation.Navigation 
 
// TODO: Rename parameter arguments, choose names that match 
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER 
private const val ARG_PARAM1 = "param1" 
private const val ARG_PARAM2 = "param2" 
 
class quest1 : Fragment() { 
    // TODO: Rename and change types of parameters 
    private var param1: String? = null 
    private var param2: String? = null 
 
    override fun onCreate(savedInstanceState: Bundle?) { 
        super.onCreate(savedInstanceState) 
        arguments?.let { 
            param1 = it.getString(ARG_PARAM1) 
            param2 = it.getString(ARG_PARAM2) 
        } 
    } 
 
    override fun onCreateView( 
        inflater: LayoutInflater, container: ViewGroup?, 
        savedInstanceState: Bundle? 
    ): View? { 
        var view:View= inflater.inflate(R.layout.fragment_quest1, container, false) 
        var btn:Button=view.findViewById(R.id.next) 
        var rg:RadioGroup=view.findViewById(R.id.radioGroup) 
        btn.setOnClickListener{ 
            if(rg.checkedRadioButtonId==R.id.radio1) 
                MainActivity.count++ 
            Navigation.findNavController(view).navigate(R.id.action_quest1_to_quest2) 
        } 
        return view 
    } 
 
    companion object { 
        @JvmStatic 
        fun newInstance(param1: String, param2: String) = 
            quest1().apply { 
                arguments = Bundle().apply { 
                    putString(ARG_PARAM1, param1) 
                    putString(ARG_PARAM2, param2) 
                } 
            } 
    } 
} 
fragment_quest1.xml: 
<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout 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=".quest1"> 
 
    <androidx.constraintlayout.widget.ConstraintLayout 
        android:layout_width="match_parent" 
        android:layout_height="match_parent"> 
 
        <TextView 
            android:id="@+id/textView" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:text="welcome to quiz" 
            android:textColor="#000000" 
            android:textSize="28dp" 
            app:layout_constraintBottom_toBottomOf="parent" 
            app:layout_constraintEnd_toEndOf="parent" 
            app:layout_constraintStart_toStartOf="parent" 
            app:layout_constraintTop_toTopOf="parent" 
            app:layout_constraintVertical_bias="0.059" /> 
 
        <TextView 
            android:id="@+id/quest" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:text="Quest1 : who is chiranjeevi" 
            android:textColor="#000000" 
            android:textSize="28dp" 
            app:layout_constraintBottom_toBottomOf="parent" 
            app:layout_constraintEnd_toEndOf="parent" 
            app:layout_constraintStart_toStartOf="parent" 
            app:layout_constraintTop_toTopOf="parent" 
            app:layout_constraintVertical_bias="0.18" /> 
 
        <RadioGroup 
            android:id="@+id/radioGroup" 
            android:layout_width="180dp" 
            android:layout_height="wrap_content" 
            app:layout_constraintBottom_toBottomOf="parent" 
            app:layout_constraintEnd_toEndOf="parent" 
            app:layout_constraintHorizontal_bias="0.168" 
            app:layout_constraintStart_toStartOf="parent" 
            app:layout_constraintTop_toBottomOf="@+id/quest" 
            app:layout_constraintVertical_bias="0.114"> 
 
            <RadioButton 
                android:id="@+id/radio1" 
                android:layout_width="match_parent" 
                android:layout_height="wrap_content" 
                android:text="actor" /> 
 
            <RadioButton 
                android:id="@+id/radio2" 
                android:layout_width="match_parent" 
                android:layout_height="wrap_content" 
                android:text="cricketer" /> 
 
            <RadioButton 
                android:id="@+id/radio3" 
                android:layout_width="match_parent" 
                android:layout_height="wrap_content" 
                android:text="comedian" /> 
 
            <RadioButton 
                android:id="@+id/radio4" 
                android:layout_width="match_parent" 
                android:layout_height="wrap_content" 
                android:text="Prime minister" 
                app:layout_constraintBottom_toBottomOf="parent" 
                app:layout_constraintTop_toTopOf="parent" /> 
        </RadioGroup> 
 
        <Button 
            android:id="@+id/next" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:text="next" 
            app:layout_constraintBottom_toBottomOf="parent" 
            app:layout_constraintEnd_toEndOf="parent" 
            app:layout_constraintHorizontal_bias="0.498" 
            app:layout_constraintStart_toStartOf="parent" 
            app:layout_constraintTop_toBottomOf="@+id/radioGroup" 
            app:layout_constraintVertical_bias="0.361" /> 
    </androidx.constraintlayout.widget.ConstraintLayout> 
</FrameLayout> 
quest2.kt: 
package com.example.quizapp 
 
import android.os.Bundle 
import androidx.fragment.app.Fragment 
import android.view.LayoutInflater 
import android.view.View 
import android.view.ViewGroup 
import android.widget.Button 
import android.widget.RadioGroup 
import androidx.navigation.Navigation 
 
// TODO: Rename parameter arguments, choose names that match 
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER 
private const val ARG_PARAM1 = "param1" 
private const val ARG_PARAM2 = "param2" 
 
/** 
 * A simple [Fragment] subclass. 
 * Use the [quest2.newInstance] factory method to 
 * create an instance of this fragment. 
 */ 
class quest2 : Fragment() { 
    // TODO: Rename and change types of parameters 
    private var param1: String? = null 
    private var param2: String? = null 
 
    override fun onCreate(savedInstanceState: Bundle?) { 
        super.onCreate(savedInstanceState) 
        arguments?.let { 
            param1 = it.getString(ARG_PARAM1) 
            param2 = it.getString(ARG_PARAM2) 
        } 
    } 
 
    override fun onCreateView( 
        inflater: LayoutInflater, container: ViewGroup?, 
        savedInstanceState: Bundle? 
    ): View? { 
        // Inflate the layout for this fragment 
        var view:View=inflater.inflate(R.layout.fragment_quest2, container, false) 
        var btn: Button =view.findViewById(R.id.next) 
        var rg: RadioGroup =view.findViewById(R.id.radioGroup) 
        btn.setOnClickListener{ 
            if(rg.checkedRadioButtonId==R.id.radio2) 
                MainActivity.count++ 
            Navigation.findNavController(view).navigate(R.id.action_quest2_to_quest3) 
        } 
        return view 
    } 
 
    companion object { 
        /** 
         * Use this factory method to create a new instance of 
         * this fragment using the provided parameters. 
         * 
         * @param param1 Parameter 1. 
         * @param param2 Parameter 2. 
         * @return A new instance of fragment quest2. 
         */ 
        // TODO: Rename and change types and number of parameters 
        @JvmStatic 
        fun newInstance(param1: String, param2: String) = 
            quest2().apply { 
                arguments = Bundle().apply { 
                    putString(ARG_PARAM1, param1) 
                    putString(ARG_PARAM2, param2) 
                } 
            } 
    } 
} 
fragment_quest2.xml: 
<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout 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=".quest2"> 
 
    <androidx.constraintlayout.widget.ConstraintLayout 
        android:layout_width="match_parent" 
        android:layout_height="match_parent"> 
 
        <TextView 
            android:id="@+id/quest" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:text="Quest2 : who is virat Kohli" 
            android:textColor="#000000" 
            android:textSize="28dp" 
            app:layout_constraintBottom_toBottomOf="parent" 
            app:layout_constraintEnd_toEndOf="parent" 
            app:layout_constraintStart_toStartOf="parent" 
            app:layout_constraintTop_toTopOf="parent" 
            app:layout_constraintVertical_bias="0.18" /> 
 
        <RadioGroup 
            android:id="@+id/radioGroup" 
            android:layout_width="180dp" 
            android:layout_height="wrap_content" 
            app:layout_constraintBottom_toBottomOf="parent" 
            app:layout_constraintEnd_toEndOf="parent" 
            app:layout_constraintHorizontal_bias="0.168" 
            app:layout_constraintStart_toStartOf="parent" 
            app:layout_constraintTop_toBottomOf="@+id/quest" 
            app:layout_constraintVertical_bias="0.114"> 
 
            <RadioButton 
                android:id="@+id/radio1" 
                android:layout_width="match_parent" 
                android:layout_height="wrap_content" 
                android:text="actor" /> 
 
            <RadioButton 
                android:id="@+id/radio2" 
                android:layout_width="match_parent" 
                android:layout_height="wrap_content" 
                android:text="cricketer" /> 
 
            <RadioButton 
                android:id="@+id/radio3" 
                android:layout_width="match_parent" 
                android:layout_height="wrap_content" 
                android:text="comedian" /> 
 
            <RadioButton 
                android:id="@+id/radio4" 
                android:layout_width="match_parent" 
                android:layout_height="wrap_content" 
                android:text="Prime minister" 
                app:layout_constraintBottom_toBottomOf="parent" 
                app:layout_constraintTop_toTopOf="parent" /> 
        </RadioGroup> 
 
        <Button 
            android:id="@+id/next" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:text="next" 
            app:layout_constraintBottom_toBottomOf="parent" 
            app:layout_constraintEnd_toEndOf="parent" 
            app:layout_constraintHorizontal_bias="0.498" 
            app:layout_constraintStart_toStartOf="parent" 
            app:layout_constraintTop_toBottomOf="@+id/radioGroup" 
            app:layout_constraintVertical_bias="0.361" /> 
    </androidx.constraintlayout.widget.ConstraintLayout> 
 
</FrameLayout> 
 
quest3.kt: 
package com.example.quizapp 
 
import android.os.Bundle 
import androidx.fragment.app.Fragment 
import android.view.LayoutInflater 
import android.view.View 
import android.view.ViewGroup 
import android.widget.Button 
import android.widget.RadioGroup 
import androidx.navigation.Navigation 
 
// TODO: Rename parameter arguments, choose names that match 
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER 
private const val ARG_PARAM1 = "param1" 
private const val ARG_PARAM2 = "param2" 
 
/** 
 * A simple [Fragment] subclass. 
 * Use the [quest3.newInstance] factory method to 
 * create an instance of this fragment. 
 */ 
class quest3 : Fragment() { 
    // TODO: Rename and change types of parameters 
    private var param1: String? = null 
    private var param2: String? = null 
 
    override fun onCreate(savedInstanceState: Bundle?) { 
        super.onCreate(savedInstanceState) 
        arguments?.let { 
            param1 = it.getString(ARG_PARAM1) 
            param2 = it.getString(ARG_PARAM2) 
        } 
    } 
 
    override fun onCreateView( 
        inflater: LayoutInflater, container: ViewGroup?, 
        savedInstanceState: Bundle? 
    ): View? { 
        // Inflate the layout for this fragment 
        var view:View= inflater.inflate(R.layout.fragment_quest3, container, false) 
        var btn: Button =view.findViewById(R.id.next) 
        var rg: RadioGroup =view.findViewById(R.id.radioGroup) 
        btn.setOnClickListener{ 
            if(rg.checkedRadioButtonId==R.id.radio3) 
                MainActivity.count++ 
            Navigation.findNavController(view).navigate(R.id.action_quest3_to_quest4) 
        } 
        return view 
    } 
 
    companion object { 
        /** 
         * Use this factory method to create a new instance of 
         * this fragment using the provided parameters. 
         * 
         * @param param1 Parameter 1. 
         * @param param2 Parameter 2. 
         * @return A new instance of fragment quest3. 
         */ 
        // TODO: Rename and change types and number of parameters 
        @JvmStatic 
        fun newInstance(param1: String, param2: String) = 
            quest3().apply { 
                arguments = Bundle().apply { 
                    putString(ARG_PARAM1, param1) 
                    putString(ARG_PARAM2, param2) 
                } 
            } 
    } 
} 
fragment_quest3.xml: 
<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout 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=".quest3"> 
 
    <androidx.constraintlayout.widget.ConstraintLayout 
        android:layout_width="match_parent" 
        android:layout_height="match_parent"> 
 
 
        <TextView 
            android:id="@+id/quest" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:text="Quest3 : who is bramhanandham" 
            android:textColor="#000000" 
            android:textSize="28dp" 
            app:layout_constraintBottom_toBottomOf="parent" 
            app:layout_constraintEnd_toEndOf="parent" 
            app:layout_constraintStart_toStartOf="parent" 
            app:layout_constraintTop_toTopOf="parent" 
            app:layout_constraintVertical_bias="0.18" /> 
 
        <RadioGroup 
            android:id="@+id/radioGroup" 
            android:layout_width="180dp" 
            android:layout_height="wrap_content" 
            app:layout_constraintBottom_toBottomOf="parent" 
            app:layout_constraintEnd_toEndOf="parent" 
            app:layout_constraintHorizontal_bias="0.168" 
            app:layout_constraintStart_toStartOf="parent" 
            app:layout_constraintTop_toBottomOf="@+id/quest" 
            app:layout_constraintVertical_bias="0.114"> 
 
            <RadioButton 
                android:id="@+id/radio1" 
                android:layout_width="match_parent" 
                android:layout_height="wrap_content" 
                android:text="actor" /> 
 
            <RadioButton 
                android:id="@+id/radio2" 
                android:layout_width="match_parent" 
                android:layout_height="wrap_content" 
                android:text="cricketer" /> 
 
            <RadioButton 
                android:id="@+id/radio3" 
                android:layout_width="match_parent" 
                android:layout_height="wrap_content" 
                android:text="comedian" /> 
 
            <RadioButton 
                android:id="@+id/radio4" 
                android:layout_width="match_parent" 
                android:layout_height="wrap_content" 
                android:text="Prime minister" 
                app:layout_constraintBottom_toBottomOf="parent" 
                app:layout_constraintTop_toTopOf="parent" /> 
        </RadioGroup> 
 
        <Button 
            android:id="@+id/next" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:text="next" 
            app:layout_constraintBottom_toBottomOf="parent" 
            app:layout_constraintEnd_toEndOf="parent" 
            app:layout_constraintHorizontal_bias="0.498" 
            app:layout_constraintStart_toStartOf="parent" 
            app:layout_constraintTop_toBottomOf="@+id/radioGroup" 
            app:layout_constraintVertical_bias="0.361" /> 
    </androidx.constraintlayout.widget.ConstraintLayout> 
 
</FrameLayout> 
quest4.kt: 
package com.example.quizapp 
 
import android.os.Bundle 
import androidx.fragment.app.Fragment 
import android.view.LayoutInflater 
import android.view.View 
import android.view.ViewGroup 
import android.widget.Button 
import android.widget.RadioGroup 
import androidx.navigation.Navigation 
 
// TODO: Rename parameter arguments, choose names that match 
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER 
private const val ARG_PARAM1 = "param1" 
private const val ARG_PARAM2 = "param2" 
 
/** 
 * A simple [Fragment] subclass. 
 * Use the [quest4.newInstance] factory method to 
 * create an instance of this fragment. 
 */ 
class quest4 : Fragment() { 
    // TODO: Rename and change types of parameters 
    private var param1: String? = null 
    private var param2: String? = null 
 
    override fun onCreate(savedInstanceState: Bundle?) { 
        super.onCreate(savedInstanceState) 
        arguments?.let { 
            param1 = it.getString(ARG_PARAM1) 
            param2 = it.getString(ARG_PARAM2) 
        } 
    } 
 
    override fun onCreateView( 
        inflater: LayoutInflater, container: ViewGroup?, 
        savedInstanceState: Bundle? 
    ): View? { 
        // Inflate the layout for this fragment 
        var view:View= inflater.inflate(R.layout.fragment_quest4, container, false) 
        var btn: Button =view.findViewById(R.id.next) 
        var rg: RadioGroup =view.findViewById(R.id.radioGroup) 
        btn.setOnClickListener{ 
            if(rg.checkedRadioButtonId==R.id.radio4) 
                MainActivity.count++ 
            if(MainActivity.count==4) 
                Navigation.findNavController(view).navigate(R.id.action_quest4_to_win2) 
            else 
                Navigation.findNavController(view).navigate(R.id.action_quest4_to_lose) 
        } 
        return view 
    } 
 
    companion object { 
        /** 
         * Use this factory method to create a new instance of 
         * this fragment using the provided parameters. 
         * 
         * @param param1 Parameter 1. 
         * @param param2 Parameter 2. 
         * @return A new instance of fragment quest4. 
         */ 
        // TODO: Rename and change types and number of parameters 
        @JvmStatic 
        fun newInstance(param1: String, param2: String) = 
            quest4().apply { 
                arguments = Bundle().apply { 
                    putString(ARG_PARAM1, param1) 
                    putString(ARG_PARAM2, param2) 
                } 
            } 
    } 
} 
fragment_quest4.xml: 
<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout 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=".quest4"> 
 
    <androidx.constraintlayout.widget.ConstraintLayout 
        android:layout_width="match_parent" 
        android:layout_height="match_parent"> 
 
 
        <TextView 
            android:id="@+id/quest" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:text="Quest4 : who is narendra modi" 
            android:textColor="#000000" 
            android:textSize="28dp" 
            app:layout_constraintBottom_toBottomOf="parent" 
            app:layout_constraintEnd_toEndOf="parent" 
            app:layout_constraintStart_toStartOf="parent" 
            app:layout_constraintTop_toTopOf="parent" 
            app:layout_constraintVertical_bias="0.18" /> 
 
        <RadioGroup 
            android:id="@+id/radioGroup" 
            android:layout_width="180dp" 
            android:layout_height="wrap_content" 
            app:layout_constraintBottom_toBottomOf="parent" 
            app:layout_constraintEnd_toEndOf="parent" 
            app:layout_constraintHorizontal_bias="0.168" 
            app:layout_constraintStart_toStartOf="parent" 
            app:layout_constraintTop_toBottomOf="@+id/quest" 
            app:layout_constraintVertical_bias="0.114"> 
 
            <RadioButton 
                android:id="@+id/radio1" 
                android:layout_width="match_parent" 
                android:layout_height="wrap_content" 
                android:text="actor" /> 
 
            <RadioButton 
                android:id="@+id/radio2" 
                android:layout_width="match_parent" 
                android:layout_height="wrap_content" 
                android:text="cricketer" /> 
 
            <RadioButton 
                android:id="@+id/radio3" 
                android:layout_width="match_parent" 
                android:layout_height="wrap_content" 
                android:text="comedian" /> 
 
            <RadioButton 
                android:id="@+id/radio4" 
                android:layout_width="match_parent" 
                android:layout_height="wrap_content" 
                android:text="Prime minister" 
                app:layout_constraintBottom_toBottomOf="parent" 
                app:layout_constraintTop_toTopOf="parent" /> 
        </RadioGroup> 
 
        <Button 
            android:id="@+id/next" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:text="submit" 
            app:layout_constraintBottom_toBottomOf="parent" 
            app:layout_constraintEnd_toEndOf="parent" 
            app:layout_constraintHorizontal_bias="0.498" 
            app:layout_constraintStart_toStartOf="parent" 
            app:layout_constraintTop_toBottomOf="@+id/radioGroup" 
            app:layout_constraintVertical_bias="0.361" /> 
    </androidx.constraintlayout.widget.ConstraintLayout> 
 
</FrameLayout> 
Navgraph: 
fragment_win: 
fragment_lose: 
fragment_quest1: 
fragment_quest2: 
fragment_quest3: 
fragment_quest4: 
PROCEDURE TO FOLLOW IN NAVIGATION USING FRAGMENTS APP 
1) Create empty Activity 
2) Create layout folder and add “activity_main.xml” file in it 
3) How to create Navigation folder and navigation graph: 
i) 
Add dependencies in build.gradle file 
implementation("androidx.constraintlayout:constraintlayout-compose:1.0.0
alpha08") 
implementation("androidx.navigation:navigation-fragment-ktx:2.3.5") 
implementation("androidx.navigation:navigation-ui-ktx:2.3.5") 
implementation("androidx.navigation:navigation-dynamic-features
fragment:2.3.5") 
ii) 
iii) 
Create navigation folder and add nav_graph.xml in it 
Create “new destination” in nav_graph.xml then select 2 blank fragments and 
name it as “HomeFragment” and “DataFragment” 
4) Add the following steps in activity_main.xml: 
i) 
Add constraint layout 
ii) 
Drag and drop fragment container view and select “HomeFragment.kt” 
add:navGraph=”@navigation/nav_graph” 
5) Add the following steps in HomeFragment in xml: 
i) 
Add Constraint View 
ii) 
iii) 
Add Text View 
Add Button  
6) Add the following steps in DataFragment in xml: 
i) 
Add Constraint View 
ii) 
iii) 
Add Text View 
Add Button 
downloadDownload PNG downloadDownload JPEG downloadDownload SVG

Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!

Click to optimize width for Twitter