Home > Back-end >  How to send from Fragment when clicking on ImageButton to Activity?
How to send from Fragment when clicking on ImageButton to Activity?

Time:05-02

It is necessary that in the Fragment, when you click on the ImageButton, the Activity opens.I've been reading all possible resources for two days and tried different methods, but none of them work. I am writing in Kotlin. Thanks in advance)

P.S. A student, there is not much time left to submit the work, so I did not approach the study of the material in a complex way (after defending the project, I will learn everything normally later).

Fragment (fragment_add.xml):

<?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=".ui.add.AddFragment">

<ImageButton
    android:id="@ id/storageBtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="24dp"
    android:background="#00FFFFFF"
    android:src="@drawable/im_storage"
    android:onClick="startActivity"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<ImageButton
    android:id="@ id/transportationBtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="50dp"
    android:background="#00FFFFFF"
    android:src="@drawable/im_transportation"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@ id/storageBtn" />
</androidx.constraintlayout.widget.ConstraintLayout>

Fragment (AddFragment.kt):

package com.project_demo.kurzov.ui.add

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageButton
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import com.project_demo.kurzov.R
import com.project_demo.kurzov.StorageActivity

class AddFragment : Fragment() {

    companion object {
        fun newInstance() = AddFragment()

    }

    private lateinit var viewModel: AddViewModel

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        View view =  inflater.inflate(R.layout.fragment_add, container, false)
        imageButton = (ImageButton)view.findViewById(R.id.storageBtn)
        imageButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                startActivity(getActivity(),StorageActivity.class)
            }
        })
        return view}
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        viewModel = ViewModelProvider(this).get(AddViewModel::class.java)
        // TODO: Use the ViewModel
    }
}

Here I tried to follow the example. It is necessary that when the button is pressed, it is sent to the (StorageActivity).

The main Activity that hosts and manages Fragments (BottomNavigationActivity.kt):

package com.project_demo.kurzov

import android.content.Intent
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.widget.Button
import android.widget.ImageButton
import com.google.android.material.bottomnavigation.BottomNavigationView
import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.findNavController
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.setupActionBarWithNavController
import androidx.navigation.ui.setupWithNavController
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.auth.ktx.auth
import com.google.firebase.ktx.Firebase
import com.project_demo.kurzov.databinding.ActivityBottomNavigationBinding

class BottomNavigationActivity : AppCompatActivity() {
    lateinit var binding: ActivityBottomNavigationBinding
    lateinit var auth: FirebaseAuth

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityBottomNavigationBinding.inflate(layoutInflater)
        setContentView(binding.root)
        auth = Firebase.auth
        auth.currentUser

        val navView: BottomNavigationView = binding.navView

        val navController = findNavController(R.id.nav_host_fragment_activity_bottom_navigation)
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        val appBarConfiguration = AppBarConfiguration(
            setOf(
                R.id.navigation_home,
                R.id.navigation_folder,
                R.id.navigation_add_documents,
                R.id.navigation_calendar,
                R.id.navigation_menu
            )
        )
        setupActionBarWithNavController(navController, appBarConfiguration)
        navView.setupWithNavController(navController)

    }

}

Activity (activity_bottom_navigation.xml):

<?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"
    android:id="@ id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@ id/nav_view"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@color/bot_navigation"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/bottom_nav_menu" />

    <fragment
        android:id="@ id/nav_host_fragment_activity_bottom_navigation"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toTopOf="@id/nav_view"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/mobile_navigation" />

</androidx.constraintlayout.widget.ConstraintLayout>

I will be very grateful help.

CodePudding user response:

You need to create an instance of Intent object and pass it to startActivity function:

imageButton.setOnClickListener {
    startActivity(Intent(requireContext(), StorageActivity::class.java))
}

CodePudding user response:

The reason we use requireContext is because we use it in fragment, we would use "this" if we were going to use activity as well.

AddFragment.kt(fragment to Activity)

imageButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {
            startActivity(requireContext(),StorageActivity::class.java)
        }
    })

activity to activity

imageButton.setOnClickListener(new View.OnClickListener() {

    public void onClick(View view) {
        startActivity(this,StorageActivity::class.java)
    }
})
  • Related