How to start another activity in function? Kotlin - android

class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
val navigate = Intent(this,Activity2::class.java)
startActivity(navigate)
}
}
}
fun switchActivity(){
val navigate = Intent(this,Activity2::class.java)
startActivity(navigate)
}
I want to start an activity from a function and not from the main activity..
I can start an activity from main class but in function, the code doesnt work..
Please help.. I'm new to kotlin android programming..

You can also try like this
class MainActivity : AppCompatActivity() {
#SuppressLint("MissingInflatedId")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
nextActivity(this, MainActivity2())
}
private fun nextActivity(context: Context, activity: Activity) {
startActivity(Intent(context, activity::class.java))
}
}

Just use it
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
switchActivity()
}
}
fun switchActivity(){
val navigate = Intent(this#MainActivity, Activity2::class.java)
startActivity(navigate)
}

Actually i don't get your question clearly. But i'll try to answer your question based on my understanding of yours.
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
switchActivity()
// if you want to pass the class through the paramater,
// can try this
// TODO: Another Way
// switchActivityCustom(Activity2::class.java)
}
}
fun switchActivity(){
val navigate = Intent(this, Activity2::class.java)
startActivity(navigate)
}
// TODO: Another Function
// fun switchActivityCustom(destination: Class<*>,){
// val navigate = Intent(this, destination)
// startActivity(navigate)
// }
}
And the last one is, put your function inside the class (in this case: MainActivity Class)

If your function is declared at the top level, without a reference to this, you'd need to pass an instance of Activity to your function:
fun switchActivity(activity: Activity) {
val navigate = Intent(activity, Activity2::class.java)
activity.startActivity(navigate)
}

Related

Android access view binding val outside onCreate activity

I have an activity that has a button. On the button click I want to update text in text view.
I want to use ViewBinding instead of the normal findViewById
This is how I created the val binding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val binding = ActivityMainBinding.inflate(layoutInflater);
setContentView(binding.root)
binding.btnRoll.setOnClickListener {
rollDice()
}
}
Now in rollDice I want to update the text view but I'm not able to access binding which make sense because its scope is limited to onCreate() , so what is the best practice for this?
private fun rollDice() {
val random = Random().nextInt(6) + 1
binding.txt_random.setText("random")
}
You have two options.
1. Store in a property
Since the inflated content of Activity is fully bound to it's lifecycle, it's safe to keep the reference as a property
class MainActivity : AppCompatActivity() {
lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater);
setContentView(binding.root)
binding.btnRoll.setOnClickListener {
rollDice()
}
}
private fun rollDice() {
val random = Random().nextInt(6) + 1
binding.txt_random.setText("random")
}
}
2. Pass the binding to the methods
That's what I usually do, it avoids creating a property where it's not really a necessity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val binding = ActivityMainBinding.inflate(layoutInflater);
setContentView(binding.root)
binding.btnRoll.setOnClickListener {
rollDice(binding)
}
}
private fun rollDice(binding: ActivityMainBinding) {
val random = Random().nextInt(6) + 1
binding.txt_random.setText("random")
}
}
Both options are valid ways to make the binding visible to Activities methods.
Store the binding in an instance variable on the Activity. Then you have access to it from all the methods in the Activity.
As the question has accepted answer and it is already addressed but here is my approach to the viewBinding
class MainActivity : AppCompatActivity() {
private val binding by lazy{
ActivityMainBinding.inflate(layoutInflater)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(binding.root)
binding.btnRoll.setOnClickListener {
rollDice()
}
}
private fun rollDice() {
val random = Random().nextInt(6) + 1
binding.txt_random.setText("random")
}
}
I go with lazy initialization of binding so that way it is only intitialized if it is required.
More you can read about lazy initialization here
https://www.baeldung.com/kotlin/lazy-initialization

Object is not abstract and does not implement abstract member public abstract fun onClick(p0: View!): Unit

I haven't found anything over the past day that shows how to do this action, everything I've seen is with a basic button of which I am unable to replicate for use with an image button. using setOnClickListener does not seem to work at all though the only cases I found of using them were 5+ years old.
Is there a Storyboard equivalent of linking activities in Android Studio?
Here is an example I found but 7 years old.
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val myButton =
findViewById<View>(R.id.live) as ImageButton
myButton.setOnClickListener(object : OnClickListener() {
// When the button is pressed/clicked, it will run the code below
fun onClick() {
// Intent is what you use to start another activity
val intent = Intent(this, LiveActivity::class.java)
startActivity(intent)
}
})
}
}
gives the following error:
Object is not abstract and does not implement abstract member public abstract fun onClick(p0: View!): Unit defined in android.view.View.OnClickListener
The problem is that you haven't included the View parameter to your onClick override. The signature of OnClickListener.onClick includes a View (the View that was clicked) as its parameter, so onClick() (with no parameters) doesn't match that signature.
You could either add it explicitly (in which case you also need to refer to the Activity's this explicitly with ActivityName.this, as this refers to the OnClickListener otherwise):
myButton.setOnClickListener(object : View.OnClickListener {
// When the button is pressed/clicked, it will run the code below
override fun onClick(view: View) {
// Replace ActivityName with the name of your Activity that this is in
val intent = Intent(ActivityName.this, LiveActivity::class.java)
startActivity(intent)
}
})
or use Kotlin's SAM conversions to add it implicitly (I'd do this approach):
// When the button is pressed/clicked, it will run the code below
myButton.setOnClickListener { // there's an implicit view parameter as "it"
// Intent is what you use to start another activity
val intent = Intent(this, LiveActivity::class.java)
startActivity(intent)
}
Fix
Change from:
myButton.setOnClickListener(object : OnClickListener { })
to
myButton.setOnClickListener(object : View.OnClickListener { })
so method will be:
override fun onClick(v: View?) {
// Do something
}
instead yours:
fun onClick() {
}
Full code:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val myButton = findViewById<ImageButton>(R.id.live)
myButton.setOnClickListener(object : View.OnClickListener {
// When the button is pressed/clicked, it will run the code below
override fun onClick(v: View?) {
// Intent is what you use to start another activity
val intent = Intent(this, LiveActivity::class.java)
startActivity(intent)
}
})
}
}

Provide custom back navigation with fragment in kotlin

I'm trying to implement OnBackPressedCallback
I followed the explanation here
https://developer.android.com/guide/navigation/navigation-custom-back
but it's incomplete
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val callback = requireActivity().onBackPressedDispatcher.addCallback(this) {
findNavController().navigate(R.id.action_showFragment_to_homeFragment)
}
//callback.handleOnBackPressed()
}
if i try to implement OnBackPressedCallback it's not ok
class ShowFragment : Fragment(), OnBackPressedCallback() {
Thank you
here is the solution
val callback = requireActivity().onBackPressedDispatcher.addCallback(this) {
findNavController().navigate(R.id.action_showFragment_to_homeFragment)
}
requireActivity().onBackPressedDispatcher.addCallback(
this,
callback
)

Pass parameter from onCreate to onClick

Here is my kotlin/android activity:
import kotlinx.android.synthetic.main.activity_player_details.*
class PlayerDetails : AppCompatActivity(), View.OnClickListener {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_player_details)
val intent = getIntent()
val numOfPlayers = intent.getIntExtra("number_of_players", 1)
next_details.setOnClickListener(this)
}
override fun onClick(v: View?) {
for (player in 1..numOfPlayers) {
// body in here
}
}
}
I want to pass the value of numOfPlayers to my onClick method so I can use it - how can I achieve this?
Either make numOfPlayer as global variable or create separate function to call when btn is clicked and pass numOfPlayer as parameter
1. make numOfPlayer as global variable
class PlayerDetails : AppCompatActivity(), View.OnClickListener {
var numOfPlayers = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_player_details)
val intent = getIntent()
numOfPlayers = intent.getIntExtra("number_of_players", 1)
next_details.setOnClickListener(this)
}
override fun onClick(v: View?) {
for (player in 1..numOfPlayers) {
// body in here
}
}
}
2. create separate function to call when btn is clicked and pass numOfPlayer as parameter
class PlayerDetails : AppCompatActivity(), View.OnClickListener {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_player_details)
val intent = getIntent()
val numOfPlayers = intent.getIntExtra("number_of_players", 1)
bottomNav.setOnClickListener {
onNextClick(numOfPlayers)
}
}
private fun onNextClick(numOfPlayers: Int) {
for (player in 1..numOfPlayers) {
// body in here
}
}
}
Define your variable with global scope like below
var numOfPlayers = 0
Make sure to define it above onCreate() method.
onCreate is called when the activity is created, and onClick is called when the next_details button is clicked, which definitely happens after onCreate.
So make numOfPlayers a class member and assign the value to it in onCreate, and also add the lateinit keyword to numOfPlayers so you don't need to make it nullable.
The code should look like this:
import kotlinx.android.synthetic.main.activity_player_details.*
class PlayerDetails : AppCompatActivity(), View.OnClickListener {
lateinit var numOfPlayers: Integer
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_player_details)
val intent = getIntent()
numOfPlayers = intent.getIntExtra("number_of_players", 1)
next_details.setOnClickListener(this)
}
override fun onClick(v: View?) {
for (player in 1..numOfPlayers) {
// body in here
}
}
}
Make numOfPlayers global
var numOfPlayers =0
In onCreate()
val intent = getIntent()
numOfPlayers = intent.getIntExtra("number_of_players", 1)
In onClick()
for (i in 1..numOfPlayers) {
// body in here
}

How to convert string to edit text box in android in intent?

How to convert a string to EditText , i make a EditText in one activity and then make a second activity and i get this edittext through the intent in the string then
how to assign it to the second activity's edittext , but i don't find it how to assign string to edittext.Here i used a go function that is used when onclick event is occured of button
This is my first activity
class MainActivity : AppCompatActivity() , View.OnClickListener{
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
fun go(v:View){
val intent=Intent(this#MainActivity,Main2Activity::class.java)
var aa=EditText1.text
intent.putExtra("name",aa)
startActivity(intent)
}
}
This is my second activity
class Main2Activity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main2)
var str=intent.getStringExtra("name").toString()
et21.setText(str)
}
}
In your first activity change go function as below,
fun go(v:View){
val intent=Intent(this#MainActivity,Main2Activity::class.java)
var aa=EditText1.text.toString()
intent.putExtra("name",aa)
startActivity(intent)
}
In your second activity change the last line of onCreate as below, you need to unwrap the value from an nullable type. Which can be done using let block over nullable type. Also I am assuming et21 is reference to the edit text and is properly initialised.
This solution is null safe
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main2)
intent.getStringExtra("name")?.let {
et21.setText(it)
}
}
Its because you was trying to set charSequence to your EditText , just convert to String while getting value from FirstEditText.
inside your Second Activity -
class Main2Activity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main2)
intent.getStringExtra("name")?.let {
et21.setText(it)
}
}
}
Inside your first activity
fun go(v:View){
val intent=Intent(this#MainActivity,Main2Activity::class.java)
var aa=EditText1.text.toString()
intent.putExtra("name",aa)
startActivity(intent)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main2)
val extras:Bundle =intent.extras
val nn=extras.getString("name")
eT21.setText("${nn}")
}
Or
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main2)
eT21.setText(intent.getStringExtra("name"))
}

Categories

Resources