Navigating to fragment doesn't Work - Kotlin - android

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.

Related

Function onCreate not execute on activity start Kotlin Android

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);
}

Handling different startDestination in android single activity Navigation graph

I want to implement a single activity project.
After showing splash i want to navigate to HomeFragment or LoginFragment based on user's login condition. In the MainActivity :
override fun onCreate(savedInstanceState: Bundle?) {
val splashScreen = installSplashScreen()
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
if (userLoggedIn()) {
navigateToHomeScreen()
} else {
navigateToLoginScreen()
}
}
The question is how should i set the navigation xml? Which one of the fragments should be the startDestionation. What's the best approach for a smooth splash/login( sign-in/sign-up/forget-pass)/ home flow.

Navigation component: Retain same fragment after orientation change

as I said in the title, I would like to keep my current fragment after rotating the phone. I could actually make it happen by adding
android:configChanges="orientation|screenSize|screenLayout|keyboardHidden"
in the Manifest. The problem is that my layout configuration is lost every time.
What am I doing wrong? How can I fix it?
I'm using Jetpack Navigation component. Exist a way to navigateToCurrentFragment()?
Edit: Part of onCreate() from Activity()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityBinding.inflate(layoutInflater)
setContentView(binding.root)
val graphInflater = (supportFragmentManager.findFragmentById(R.id.mNavHost) as NavHostFragment).navController.navInflater
val navGraph = graphInflater.inflate(R.navigation.services_nav_graph)
navGraph.startDestination = R.id.FirstFragment
(supportFragmentManager.findFragmentById(R.id.mNavHost) as NavHostFragment).navController.graph = navGraph
}

Disable multiple instance of fragment

In my main activity when user clicks button it shows him specific fragment. But if he clicks it again it adds another instance to backstack. And then the user needs to click back many times as click on the button.
class AppActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_app)
SharedPreferenceHelper.init(this)
GRPCClient.init(this)
DataBaseHelper.init(this)
imageViewDot.setOnClickListener {
findNavController(this, R.id.navHostFragmentApp).navigate(R.id.syncFragment)
}
}}
How can I prevent it from happening. What I need to do is if fragment is visible the button will not do anything.
try to flag that you already added this fragment and don't do that again
class AppActivity : AppCompatActivity() {
var navigated: Boolean = false
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_app)
SharedPreferenceHelper.init(this)
GRPCClient.init(this)
DataBaseHelper.init(this)
imageViewDot.setOnClickListener {
if(navigated) return
findNavController(this, R.id.navHostFragmentApp).navigate(R.id.syncFragment)
navigated = true
}
}
override fun onBackPressed() {
super.onBackPressed() // removes from back stack if present in there
navigated = false
}
}
or you can use getBackStackEntry or getCurrentBackStackEntry from NavController instance to check that this Fragment is already on the position

Cannot resolve settings_container

The Android official page says to create the preferences Activity this way:
class MySettingsActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
supportFragmentManager
.beginTransaction()
.replace(R.id.settings_container, MySettingsFragment())
.commit()
}
}
When I do that, R.id.settings_container cannot be resolved. Should I simply use the automatic option "Create id value resource 'settings_container'", or that resource should be created somehow else to be meaningful?
use android.R.id.content (which is used in onCreate() also)

Categories

Resources