I want to create a button that allows me to both reload my activity and call a new function once the activity is reloaded. Unfortunately by calling two functions at the same time the second function I call after the activity refreshes does not work. How can I solve this problem which seems simple at first sight
fun newPie(valeur: Double){
config.addData(SimplePieInfo(valeur, Color.parseColor("#000000")))
config.drawText(false)
config.strokeMode(false)
anim.applyConfig(config)
anim.start()}
fun refresh() {
val intent = Intent(applicationContext, anychart::class.java)
startActivity(intent)
finish()
}
button.setOnClickListener(){
refresh()
newPie(valeur = 33.3)
}
CodePudding user response:
If you want to call the new function once activity is reloaded, you should call that function into onCreate method of activity.
override fun onCreate(...) {
...
newPie(valeur = 33.3)
}
and button should only be used for refreshing the activity:
fun refresh() {
val intent = Intent(applicationContext, anychart::class.java)
startActivity(intent)
finish()
}
button.setOnClickListener(){
refresh()
}
CodePudding user response:
As you want to restart the activity and hit the function then you should finish the activity, pass data to new instance of the activity so that you can check and trigger the function
Following code will help you
- When you are finishing the activity just dont restart the activity but send some data with it as well.
var intent = Intent(this, anychart::class.java)
intent.putExtra("startFunction", true) // to trigger the function or not
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK // so any pending activity can be removed
intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TASK
startActivity(intent)
Then in the onCreate function check the data and trigger function when the true is sent
intent?.extras?.let {
if(it.containsKey("startFunction")){
val isStart = it.getBoolean("Data", false)
if(isStart){
newPie(valeur = 33.3)
}
}
}
CodePudding user response:
The extra function call needs to happen in the newly created Activity instance, and it has to be done after onCreate()
has been called. You could directly put this function call in onCreate()
, but if you don't want it to be called the very first time the Activity is opened, then you need to add an extra to your Intent and use that extra to determine if the function should be called. Like this:
companion object {
private const val IS_REFRESH_KEY = "is_refresh"
}
override fun onCreate(savedInstanceState: Bundle) {
super.onCreate(savedInstanceState)
// ...
button.setOnClickListener(){
refresh()
}
val isRefresh = intent.extras?.getInt(IS_REFRESH_KEY) == 1
if (isRefresh) {
newPie(valeur = 33.3)
}
}
fun refresh() {
val intent = Intent(applicationContext, anychart::class.java).apply {
putExtra(IS_REFRESH_KEY, 1)
}
startActivity(intent)
finish()
}
If you want the 33.3 to be customizable, you could use a Float extra instead:
companion object {
private const val REFRESH_PIE_VALUE = "refresh_pie_value"
}
override fun onCreate(savedInstanceState: Bundle) {
super.onCreate(savedInstanceState)
// ...
button.setOnClickListener(){
refresh()
}
val refreshPieValue = intent.extras?.getFloat(REFRESH_PIE_VALUE) ?: -1f
if (refreshPieValue >= 0f) {
newPie(refreshPieValue)
}
}
fun refresh() {
val intent = Intent(applicationContext, anychart::class.java).apply {
putExtra(REFRESH_PIE_VALUE, 33.3) // customize the value here
}
startActivity(intent)
finish()
}