-1

can i call another Activity From Fragment Button ? I have seen all the problems in stackoverflow, but have not found a solution.

this my fragment code from MainActivity

package com.example.apptest

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import kotlinx.android.synthetic.main.fragment_home.*

// 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 [HomeFragment.newInstance] factory method to
 * create an instance of this fragment.
 */
class HomeFragment : 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
        return inflater.inflate(R.layout.fragment_home, container, false)
        

    }




    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 HomeFragment.
         */
        // TODO: Rename and change types and number of parameters
        @JvmStatic
        fun newInstance(param1: String, param2: String) =
            HomeFragment().apply {
                arguments = Bundle().apply {
                    putString(ARG_PARAM1, param1)
                    putString(ARG_PARAM2, param2)
                }
            }
    }
}

and this is my fragment xml code

<?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=".HomeFragment">

    <TextView
        android:textColor="#34495e"
        android:textSize="20sp"
        android:layout_marginTop="100dp"
        android:id="@+id/mytitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="sans-serif"
        android:text="BELAJAR BERSAMA NATSUZD"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:textColor="#34495e"
        android:textSize="20sp"
        android:id="@+id/japantitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="日本語"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/mytitle" />

    <Button
        style="@style/Widget.AppCompat.Button.Colored"
        android:textColor="#34495e"
        android:layout_marginTop="50dp"
        android:id="@+id/buttonImage"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginLeft="50dp"
        android:layout_marginRight="50dp"
        android:background="@drawable/custome_button"
        android:drawableStart="@drawable/book50"
        android:drawableLeft="@drawable/book50"
        android:gravity="center"
        android:padding="15dp"
        android:text="KOSAKATA"
        android:textSize="15sp"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/japantitle"
        tools:ignore="OnClick" />

    <Button
        style="@style/Widget.AppCompat.Button.Colored"
        android:textColor="#34495e"
        android:text="BELAJAR SOAL"
        android:gravity="center"
        android:textSize="15sp"
        android:textStyle="bold"
        android:padding="15dp"
        android:drawablePadding="15dp"
        android:drawableLeft="@drawable/exam"
        android:layout_marginRight="50dp"
        android:layout_marginLeft="50dp"
        android:layout_marginTop="20dp"
        android:id="@+id/buttonImage1"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@drawable/custome_button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/buttonImage"
        android:drawableStart="@drawable/exam" />

    <Button
        style="@style/Widget.AppCompat.Button.Colored"
        android:textColor="#34495e"
        android:text="PERCAKAPAN"
        android:gravity="center"
        android:textSize="15sp"
        android:padding="15dp"
        android:textStyle="bold"
        android:drawablePadding="15dp"
        android:drawableLeft="@drawable/chat"
        android:layout_marginRight="50dp"
        android:layout_marginLeft="50dp"
        android:layout_marginTop="20dp"
        android:id="@+id/buttonImage2"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@drawable/custome_button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/buttonImage1"
        android:drawableStart="@drawable/chat" />

    <Button
        style="@style/Widget.AppCompat.Button.Colored"
        android:textColor="#34495e"
        android:text="POLA KALIMAT"
        android:gravity="center"
        android:textSize="15sp"
        android:padding="15dp"
        android:textStyle="bold"
        android:drawablePadding="15dp"
        android:drawableLeft="@drawable/variable"
        android:layout_marginRight="50dp"
        android:layout_marginLeft="50dp"
        android:layout_marginTop="20dp"
        android:id="@+id/buttonImage3"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@drawable/custome_button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/buttonImage2"
        android:drawableStart="@drawable/variable" />


</androidx.constraintlayout.widget.ConstraintLayout>

and this is my second activity ( target activity )

package com.example.apptest

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle

class SoalActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_soal)
    }
}

how do I connect to the second activity using the button in the fragment

Rangga
  • 31
  • 5
  • Just use startActivity(intent) – Gabriele Mariotti Jul 18 '20 at 12:06
  • You should search for how to run activity from a fragment, startActivity(context, SoalActivity::class.java) – FarshidABZ Jul 18 '20 at 12:07
  • ``` buttonImage.setOnClickListener { val intent = Intent(context,SoalActivity::class.java) startActivity(intent) } ``` I put this code into onCreateView on HomeFragment.kt But Not Working – Rangga Jul 18 '20 at 12:24
  • yes, that's the answer, it's just using java and yesterday I didn't know that there was already a feature to change the programming language automatically – Rangga Jul 19 '20 at 15:11

1 Answers1

1

Sure you can.

In order to launch an activity from your fragment, override the onViewCreated() method in your fragment. Inside it, type:

buttonImage2.setOnClickListener {
    val intent = Intent(context, SoalActivity::class.java)
    startActivity(intent)
}

And that's it :)

Lheonair
  • 468
  • 5
  • 14
  • Just to clarify what happened in your coments above: This won't work inside onCreateView(). It needs to be done in onViewCreated() – Lheonair Jul 18 '20 at 12:47