Created
September 26, 2022 07:29
-
-
Save mikkipastel/a50cc233a557c082e762f9629c0fb5a3 to your computer and use it in GitHub Desktop.
activity & fragment workshop : refactor code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.ascedncorp.androidbasic | |
import android.os.Bundle | |
import androidx.appcompat.app.AppCompatActivity | |
import com.ascedncorp.androidbasic.databinding.ActivityMainBinding | |
class MainActivity: AppCompatActivity() { | |
private val binding: ActivityMainBinding by lazy { | |
ActivityMainBinding.inflate(layoutInflater) | |
} | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(binding.root) | |
binding.button.setOnClickListener { | |
startActivity(SecondActivity.newIntent(this, binding.edittext.text.toString())) | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.ascedncorp.androidbasic | |
import android.content.Context | |
import android.content.Intent | |
import android.os.Bundle | |
import androidx.appcompat.app.AppCompatActivity | |
import com.ascedncorp.androidbasic.databinding.ActivitySecondBinding | |
class SecondActivity: AppCompatActivity() { | |
companion object { | |
const val EXTRA_TEXT = "SecondActivity:EXTRA_TEXT" | |
fun newIntent(context: Context, text: String): Intent { | |
return Intent(context, SecondActivity::class.java).apply { | |
putExtra(EXTRA_TEXT, text) | |
} | |
} | |
} | |
private val text by lazy { | |
intent.getStringExtra(EXTRA_TEXT) | |
} | |
private val binding: ActivitySecondBinding by lazy { | |
ActivitySecondBinding.inflate(layoutInflater) | |
} | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(binding.root) | |
binding.textview.text = text | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment