Home > Software design >  How to make Button within fragment close current and open another fragment in Kotlin
How to make Button within fragment close current and open another fragment in Kotlin

Time:07-05

I need to create 2 fragments and 2 button in first fragment one which will close current fragment and start another, and second which will close app at all, what i'm doing wrong(each fragment in different holders)?

First fragment opens well, but when i click on buttons in first activity, they don't working at all.

How can i write this rightway?

MainActivity.kt

package com.example.webviewapp

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)
        supportFragmentManager
            .beginTransaction()
            .replace(R.id.fullcreen_holder, start_fragment.newInstance())
            .commit()

    }

}


   

start_fragment.kt

package com.example.webviewapp

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


class start_fragment : Fragment() {

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?

    ): View? {
        val view = inflater.inflate(R.layout.layout_start_fragment, container, false)
        val agreeButton: Button = view.findViewById(R.id.privacy_agree)
        val declineButton: Button = view.findViewById(R.id.privacy_decline)
        agreeButton.setOnClickListener{
            requireActivity().getSupportFragmentManager().beginTransaction()
                .replace(R.id.webview_holder, webview_fragment())
                .hide(start_fragment())
                .commit();

        }
        declineButton.setOnClickListener{
            android.os.Process.killProcess(android.os.Process.myPid())
        }
        return inflater!!.inflate(R.layout.layout_start_fragment, container, false)
    }



    companion object {
        @JvmStatic
        fun newInstance() = start_fragment()
    }
}

CodePudding user response:

Let's crate an android resouse directoty. That directory name is navigation. After that we create navigation resouse file. That file will have name as nav_graph.xml
The next we put it in your activity_main.xml

<androidx.fragment.app.FragmentContainerView
    android:id="@ id/nav_host_fragment"
    android:name="androidx.navigation.fragment.NavHostFragment"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:defaultNavHost="true"
    app:navGraph="@navigation/nav_graph" />

In nav_graph.xml we add destinations. It looks like

<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_host"
app:startDestination="@id/oneFragment">


<fragment
    android:id="@ id/oneFragment"
    android:name="com.uogames.androidjun.OneFragment"
    android:label="OneFragment"
    tools:layout="@layout/fragment_one" >
</fragment>

<fragment
    android:id="@ id/twoFragment"
    android:name="com.uogames.androidjun.TwoFragment"
    android:label="TwoFragment"
    tools:layout="@layout/fragment_two" />
</navigation>

Then when you start application you will start whith OneFragment. And if you call findNavController().navigate(R.id.to_twoFragment) you will see second fragment. You can read about it here.
Also you need dependencies

implementation 'androidx.navigation:navigation-fragment-ktx:2.4.2'
implementation 'androidx.navigation:navigation-ui-ktx:2.4.2'

If you want to close application just call it requireActivity().finishAndRemoveTask()

  • Related