I'm new to Android development and I just started learning.
Here's my problem:
My onCreate function in activity_main works normally, it shows everything. Then he logs in and shows me the second activity and there also the onCreate function works but when I go to the third activity the onCreate function does not execute.
DashboardActivity.kt
class DashboardActivity : AppCompatActivity() {
#SuppressLint("MissingInflatedId")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_dashboard)
val policy = ThreadPolicy.Builder().permitAll().build()
StrictMode.setThreadPolicy(policy)
sendGet()
// sendPostRequest()
}
fun openTest(view: View){
setContentView(R.layout.activity_test);
}
}
TestActivity.kt
class TestActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_test)
println("test")
}
}
Could someone guide me or explain what and how? I've been sitting on this for a while, searching the internet and nothing...
I searched the internet, tried solutions, tried to create more activities, turned on debug mode by adding breakpoint and it doesn't even enter this function
To start an Activity, you need to call the following:
startActivity(Intent(this, NextActivity::class.java))
Otherwise, onCreate(Bundle?) will not be called if you do not explicitly call the above.
So in your case, seems that you would like to get to TestActivity in openTest(View), you should have something like this:
fun openTest(view: View) {
startActivity(Intent(this, TestActivity::class.java))
// Calling setContentView() will not trigger another Activity
// setContentView(R.layout.activity_test);
}
Related
I have some initialisation kotlin code I want to run when my flutter app starts.
My app already has a MainActivity.kt that exists in app/src/main/kotlin/com/example/my_app/
I then added an onCreate to this activity and added an exception to confirm this code executed....but it did not...
class MainActivity: FlutterActivity() {
override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
super.onCreate(savedInstanceState, persistentState)
throw RuntimeException("blah");
}
}
Where can I put kotlin initialization code that I want to execute on application launch?
You use a different oncreate method android first calls this method while loading android app it is lifecycle method.
class MainActivity : FlutterActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
throw RuntimeException("blah");
}
}
What is the problem?
I want to add this custom window callback in each activity already implemented in the app, currently I need to manually modify the code of the onCreate method of each activity or make it inherit a class that already has onCreate in the desired way, but I need that this process could be performed without modifying the app's existing code, just adding some initialization.
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
window.callback = CustomCallback(window.callback, this)
}
Inside you app Application Class (you'll need to create one, if not done yet), on "onCreate" method, call
registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
#Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
//Code here will run for every activity
}
}
You need a BaseActivity
class BaseActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
//your code
}
All of your activities need to extends BaseActivity
class MainActivity : BaseActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(binding.root)
}
}
I was trying to open some fragment however the app keep crashing
I am wondering if it was possible to fix the issue that I got
class HomePage : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_home_page)
val addMed = findViewById<Button>(R.id.addMedButton)
val fragManager: FragmentManager = supportFragmentManager
fragManager.beginTransaction().add(R.id.frameLayout, addMed).commit()
}
}
You have to add a fragment, not a button.
Besides, if the button is found by id, it is already present in the activity.
What I am trying to do is restart the CountdownTimer object when it is finished. Therefor I put it inside a procedure 'goCountdown', so onFinish() it calls itself. However this gives the following problems:
A procedure goCountdown cannot be called from within the same class
B activity text cannot be updated with time from outside the MainActivity class.
See references '!!! nr !!!' in code below:
the 'goCountdown() function is found (recognized/doesnt give an
error) at place 1 even though the procedure is placed outside the class.
when goCountdown is placed inside the class at nr 2 it is not found at place nr 1 (gives error).
As it only works outside of the class, it is now impossible to update the text on the activity on every tick because the MainActivity isnt accessible.
Questions:
Why does Kotlin / Android Studio not recognize the 'goCountdown()' function when it is placed WITHIN
the same activity class?
Even if that will work, is there a way to acces the texts on the MainActivity from a top level procedure?
Besides my goal to make the timer loop I am trying to understand why it is not working. Thanks for explaining or pointing me to the explanation.
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
tapAction.setOnClickListener{
boTimeRunning = !boTimeRunning
if(boTimeRunning ){
goCountdown() //!!! 1 !!! its fine to call it from here when its outside the class
}
}
//!!! 2 !!!but if fun goCountdown() block is placed here, it is not seen at !!! 1 !!! place
}
}
fun goCountdown(){
object : CountDownTimer1(timeSettings_set * 1000, 1000){
override fun onTick(p0: Long) {
MainActivity.txtBig.text = "sometext" //!!! 3 !!!this doesnt work, also when MainActivity is declared as a variable object.
}
override fun onFinish() {
goCountdown() //primary goal: restart the timer when its done
}
}.start()
}
Is txtBig kotlin synthetic view or a local variable or global variable?
if txtBig is kotlin synthetic view, you should be able to call it.
if txtBig isn't kotlin synthetic veiw, try this code.
class MainActivity : AppCompatActivity() {
private lateinit var txtBigcopy: TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
txtBig = findViewById<TextView>(R.id.txt_big) // Your id in xml
txtBigcopy = txtBig
tapAction.setOnClickListener{
boTimeRunning = !boTimeRunning
if(boTimeRunning ){
goCountdown()
}
}
}
fun goCountdown(){
object : CountDownTimer1(timeSettings_set * 1000, 1000){
override fun onTick(p0: Long) {
txtBigcopy.text = "sometext"
}
override fun onFinish() {
goCountdown()
}
}.start()
}
}
I am not really sure if my case is just not possible with late init properties. But let me just ask :)
I have an applicationController which is used in nearly every activity - so i created a BaseActivity
The problem now is that when I want to get dependencies from the application controller in the child activity, I get an Uninitialized Exception.
Thanks for your help!
Because you override wrong method in BaseActivity, that why your app crash.
Solution: Change your code to
abstract class BaseActivity : AppCompatActivity() {
lateinit var applicationController: ApplicationController
// [IMPORTANT] Remove or comment-out this method
// override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
// super.onCreate(savedInstanceState, persistentState)
// applicationController = ApplicationController.getInstance(applicationContext)
// }
// Override this method
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
applicationController = ApplicationController.getInstance(applicationContext)
}
}
Explanation: This section will explain why the code is not working.
kotlin.UninitializedPropertyAccessException
This is a sub-class of RuntimeException, the app will throw this
exception when you access an object by calling properties or methods
on its own before initializing.
When LoginScreen activity is created, Android will call its onCreate method.
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_login_screen)
emailMatcher = applicationController.getEmailMatcher()
passwordMatcher = applicationController.getPasswordMatcher()
}
The first line super.onCreate(savedInstanceState) will call onCreate method of its parent, in this case onCreate (bundle) in BaseActivity activity will be called.
Unfortunately, in BaseActivity activity, you override and put the initial code for applicationController in another method onCreate(bundle, persistentState) which quite similar to onCreate (bundle). The difference between them is number of params. As a result, at this time applicationController is still not initialized.
override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
super.onCreate(savedInstanceState, persistentState)
applicationController = ApplicationController.getInstance(applicationContext)
}
Until the app reach this line
emailMatcher = applicationController.getEmailMatcher()
Because you call getEmailMatcher method on an uninitialized object applicationController, so the app throws kotlin.UninitializedPropertyAccessException and make your app crash.