Signout button in android fragements not working? Kotlin - android

package com.app.myproject.Fragments
import android.content.Intent
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.util.Log
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.app.myproject.LoginActivity
import com.google.firebase.auth.FirebaseAuth
import kotlinx.android.synthetic.main.activity_login.*
import com.app.myproject.R
import com.app.myproject.RegisterActivity
import kotlinx.android.synthetic.main.fragment_settings.*
/**
* A simple [Fragment] subclass.
*/
class SettingsFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? = inflater.inflate(R.layout.fragment_settings, container, false)
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
view.findViewById(R.id.signout_button).setOnClickListener {
FirebaseAuth.getInstance().signOut()
val intent = Intent(this#SettingsFragment.context, LoginActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK)
startActivity(intent)
}
}
}
This is my code. I'm using firebase for the user accounts. I have my settings form in a fragment like this https://prnt.sc/rvs5yn
When I click the button signout nothing happens. I was following some code I found on stackoverflow on how to get intent to work on fragments.
Any help would be greatly appreciated.

Your code is unreachable since you wrote it after the return statement, then, you have to access the inflated view from the onViewCreated method.
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? = inflater.inflate(R.layout.fragment_settings, container, false)
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
view.findViewById<View>(R.id.signout_button).setOnClickListener {
... // Paste your code here
}
}

Related

How do I use shared preferences when a button is clicked

I am working on an Android app using Android Studio.
I want to save the values input into some editText textFields.
These textFields are in a Fragment view? (sorry I am very new to Android coding).
I have tried to use SharedPreferences but keep getting the error.
(Unresolved reference: getSharedPreference).
Here is my code:
import android.content.Context.MODE_PRIVATE
import android.content.SharedPreferences
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.EditText
import androidx.core.os.bundleOf
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.findNavController
class ConnectFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_connect, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
view.findViewById<Button>(R.id.button_connect).setOnClickListener {
val serverURIFromEditText =
view.findViewById<EditText>(R.id.edittext_server_uri).text.toString()
val clientIDFromEditText =
view.findViewById<EditText>(R.id.edittext_client_id).text.toString()
val usernameFromEditText =
view.findViewById<EditText>(R.id.edittext_username).text.toString()
val pwdFromEditText =
view.findViewById<EditText>(R.id.edittext_password).text.toString()
val mqttCredentialsBundle = bundleOf(
MQTT_SERVER_URI_KEY to serverURIFromEditText,
MQTT_CLIENT_ID_KEY to clientIDFromEditText,
MQTT_USERNAME_KEY to usernameFromEditText,
MQTT_PWD_KEY to pwdFromEditText
)
findNavController().navigate(
R.id.action_ConnectFragment_to_ClientFragment,
mqttCredentialsBundle
)
val sharedPreferences: SharedPreferences =
getSharedPreferences("MyFacts", MODE_PRIVATE)
val editor = sharedPreferences.edit()
editor.putString("UserName", usernameFromEditText)
editor.apply()
}
}
}
Not sure what is up except to think that it is not in the MainActivity view but in a Fragment. But can't figure out how to resolve it. Could someone help me.

Unresolved reference: fragment

i have encountered some errors in my kotlin code.
The code is as below:
'''package com.mohamed.focuscounter
import android.os.Bundle
import androidx.fragment.app.Fragment //(line4)
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup'''
'''class AvailableFragment : Fragment() { //(line 9)
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_available, container, false)
}
}'''
The Errors encountered is as follows:
1.unresolved reference:fragment (line 4)
2.unresolved reference:Fragment (line 9)
Any help will be appreciated
Make sure you have Fragment's dependency in your app build.gradle file
implementation "androidx.fragment:fragment-ktx:1.5.5"

How to pass a Mutable list from fragment to another fragment?

I'm new to Kotlin and Fragments, actually in my App i have two fragments, Fragment1 and Fragment2
In the first fragment i have a method which get data from some editTexts and add them to a MutableList then in my Fragment2 i have a ListView in which i would be able to show that MutableList...
But how can i pass the MutableList from Fragment1 to Fragment2?
Here is my Fragment1 code:
private var listArticoli: MutableList<Articolo>? = null
private fun addBarcode(barcode: String, qta: String) {
if (barcode.isEmpty()) {
txtBarcode.requestFocus()
return;
}
listArticoli?.add(Articolo(barcode, qta.toInt()))
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
txtBarcode = view.findViewById(R.id.txtBarcode)
listArticoli = mutableListOf()
btnArticoli.setOnClickListener {
findNavController().navigate(R.id.action_FirstFragment_to_SecondFragment) // button on click of which i navigate to Fragment2
}
}
Fragment2 code:
class SecondFragment : Fragment() {
lateinit var listView: ListView
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_second, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
listView = view.findViewById(R.id.listView)
view.findViewById<Button>(R.id.btnIndietro).setOnClickListener {
findNavController().navigate(R.id.action_SecondFragment_to_FirstFragment)
}
}
}
1.Share the viewmodel with their common parent activity
2.FragmentResultListener from fragment(after 1.3.0-alpha04)
To access Mutable list in both the fragments,you can create ViewModel which consists of MutableLiveDate of you required data type.Then expose MutableLiveData from viewmodel.So use the same ViewModel in both the fragments by setting data from one fragment1 and by observing data from fragment2.
SampleViewModel.kt
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
class SampleViewModel: ViewModel() {
var mutableLiveData:MutableLiveData<MutableList<Articolo> = MutableLiveData()
}
Fragment1.kt
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
class Fragment1 : Fragment() {
private lateinit var viewModel: SampleViewModel
var arrayList: ArrayList<Articolo> = ArrayList()
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?,
): View? {
return super.onCreateView(inflater, container, savedInstanceState)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
viewModel = ViewModelProvider(requireActivity()).get(SampleViewModel::class.java)
//TODO: Add data to arraylist
viewModel.mutableLiveData.value = arrayList
}
}
Fragment2.kt
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
class Fragment2 : Fragment() {
private lateinit var viewModel: SampleViewModel
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?,
): View? {
return super.onCreateView(inflater, container, savedInstanceState)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
viewModel = ViewModelProvider(requireActivity()).get(SampleViewModel::class.java)
viewModel.mutableLiveData.observe(viewLifecycleOwner, Observer {
//TODO:Can do you requirements here
})
}
}

Button In Kotlin will not work in Fragments

So I'm trying to create a tabbed app in Kotlin and I have chosen the default one they made for you to practice but I can't figure out how to get the buttons working `
package com.example.android_app.ui.home
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 androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import com.example.android_app.R
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.fragment_home.*
class HomeFragment : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
//Program Buttons
logout.setOnClickListener{
val intent = Intent(this, sign_in::class.java)
startActivity(intent)
}
}
private lateinit var homeViewModel: HomeViewModel
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
homeViewModel =
ViewModelProviders.of(this).get(HomeViewModel::class.java)
val root = inflater.inflate(R.layout.fragment_home, container, false)
homeViewModel.text.observe(viewLifecycleOwner, Observer {
})
return root
}
}
`
As far as I know this should work. The problem is not in the button but in Intent. My button is already defined and has no errors but there is a red line under Intent even though it's imported. The error message is below.
public constructor Intent(p0: Context!, p1: Class<*>!) defined in android.content.Intent
public constructor Intent(p0: String!, p1: Uri!) defined in android.content.Intent
You need to pass context into the constructor of Intent instead of this.
val intent = Intent(context, sign_in::class.java)

I cannot setonClickListener RelativeLayout on a fragment

I have a relative layout inside a fragment. The fragment is inside MainActivity. I want to make setonClickListener event on it to open a new activity. I want to make the relative layout rlLogOut to open SignInActivity activity. But how do I put the setOnClickListener event on the fragment. Is it on the OtherFragment.kt or in the MainActivity
I've tried to make the setOnClickListener inside the MainActivity, and I know thats not the solution. I think the intent is the problem. But I don't know the syntax for it. I'm new in kotlin.
import android.content.Intent
import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import kotlinx.android.synthetic.main.fragment_others.*
// 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.
*
*/
class OtherFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
rlLogOut.setOnClickListener{
val mainIntent = Intent(
this#OtherFrament,
SignInActivity::class.java
)
startActivity(mainIntent)
}
return inflater.inflate(R.layout.fragment_others, container, false)
}
}
I expect that when the rlLogOut clicked, SignInActivity activity is opened. Thanks for helping.
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View = inflater.inflate(R.layout.fragment_others, container, false)
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
rlLogOut.setOnClickListener {
val mainIntent = Intent(requireActivity(), SignInActivity::class.java)
requireActivity().startActivity(mainIntent)
}
}

Categories

Resources