Reputation: 504
I have a "home page" in my app that looks like the following
import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import kotlinx.android.synthetic.main.activity_home.*
class HomeActivity: Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view: View = inflater.inflate(R.layout.activity_home, container, false)
return view
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
ButtonAddition.setOnClickListener{
}
}
}
I have a mainActivity that has the following code in it
import android.os.Bundle
import android.support.design.widget.NavigationView
import android.support.v4.app.Fragment
import android.support.v4.view.GravityCompat
import android.support.v7.app.ActionBarDrawerToggle
import android.support.v7.app.AppCompatActivity
import android.view.MenuItem
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.app_bar_main.*
class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(toolbar)
val toggle = ActionBarDrawerToggle(
this, drawer_layout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close
)
drawer_layout.addDrawerListener(toggle)
toggle.syncState()
nav_view.setNavigationItemSelectedListener(this)
displayScreen(-1)
}
override fun onBackPressed() {
if (drawer_layout.isDrawerOpen(GravityCompat.START)) {
drawer_layout.closeDrawer(GravityCompat.START)
} else {
super.onBackPressed()
}
}
private fun displayScreen(id: Int) {
val fragment: Fragment = when (id) {
R.id.home -> {
HomeActivity()
}
R.id.record -> {
HomeActivity()
}
R.id.pro -> {
HomeActivity()
}
R.id.about -> {
HomeActivity()
}
else -> {
HomeActivity()
}
}
supportFragmentManager.beginTransaction()
.replace(R.id.relativelayout, fragment)
.commit()
}
override fun onNavigationItemSelected(item: MenuItem): Boolean {
displayScreen(item.itemId)
drawer_layout.closeDrawer(GravityCompat.START)
return true
}
}
I also have a "AdditionSplash" fragment i am trying to switch to with the following code in it
import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
class AdditionSplash: Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view: View = inflater.inflate(R.layout.activity_addition_splash, container, false)
return view
}
}
on the homepage fragment I have an onclicklistener and I want to use that to switch to the additionsplash
fragment from the homepage fragment. ( don't want to use that navigation drawer I have for that) I can get this to work if i change the additionsplash fragment into an activity and use intents, but i can't seem to switch to a fragment from a fragment.
Is this impossible to do? I want to use that button to just swap to a different fragment.
here is the solution i came up with thanks to a suggestion
In the main activity I did:
fun chooseActivity(view: View){
val buttonClicked = view as Button
val fragment: Fragment = when (buttonClicked.id) {
R.id.ButtonAddition -> {
AdditionSplash()
}
else -> {
HomeActivity()
}
}
supportFragmentManager.beginTransaction()
.replace(R.id.relativelayout, fragment)
.commit()
}
Upvotes: 0
Views: 143
Reputation: 2427
You can just call it from host activity of your fragment, like you did it in your MainActivity:
activity?.supportFragmentManager.beginTransaction()
.replace(R.id.relativelayout, fragment)
.commit()
Upvotes: 1