Я хочу вызвать метод showNewsPageTime () из MainFragment.kt в AlertBoxActivity.kt, чтобы открыть newsFragment , но приложение имеетостановлен.
MainFragment.kt
(BottomNavigationView
находится здесь. Я хочу открыть страницу NewsFragment
отсюда, потому что, если я открою ее непосредственно, навигация снизу снизится.)
package kz.app.fragment
import android.annotation.SuppressLint
import android.app.Activity
import android.os.Bundle
import android.support.design.widget.BottomNavigationView
import android.support.v4.app.Fragment
import android.support.v4.widget.DrawerLayout
import android.support.v7.content.res.AppCompatResources
import android.support.v7.view.SupportMenuInflater
import android.view.Gravity
import android.view.LayoutInflater
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import kotlin.concurrent.schedule
import android.view.inputmethod.InputMethodManager
import kotlinx.android.synthetic.main.fragment_main.*
import kotlinx.android.synthetic.main.fragment_main.view.*
import kz.app.R
import kz.app.model.MainViewModel
import kz.app.model.Stat## Heading ##eViewModel
import kz.app.repository.PreferencesRepository
import kz.app.util.OnBackPressDelegate
import kz.app.util.bind
import kz.app.util.setBadge
class MainFragment : Fragment(), BottomNavigationView.OnNavigationItemSelectedListener, OnBackPressDelegate, DrawerLayout.DrawerListener {
private val model by lazy { StateViewModel.get<MainViewModel>(this) }
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val imm = context?.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0)
retainInstance = true
if (!PreferencesRepository.getInstance(context
?: return).isTutorialFinished.blockingFirst())
InstructionFragment().show(childFragmentManager)
}
@SuppressLint("SetTextI18n")
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.fragment_main, container, false)
view.bottomNavigation.initMenu()
view.bottomNavigation.setOnNavigationItemSelectedListener(this)
view.toolbar.setNavigationOnClickListener { view.drawer.openDrawer(Gravity.START) }
view.pickCity.setOnClickListener { _ -> CityPickerDialogFragment.show(childFragmentManager) { model.setCurrentCity(it.id) } }
view.pickCity.bind(model.currentCityIconResId) { setImageResource(it) }
view.flipper.displayedChild = savedInstanceState?.getInt(KEY_CURRENT_ITEM, 0) ?: 0
view.bottomNavigation.run {
selectedItemId = menu.getItem(view.flipper.displayedChild).itemId
}
view.drawer.addDrawerListener(this)
view.toolbar.setOnMenuItemClickListener {
val currentPage = view?.flipper?.displayedChild
?: return@setOnMenuItemClickListener false
val shoppingFragment = childFragmentManager.findFragmentById(R.id.shoppingFragment)
val newsFragment = childFragmentManager.findFragmentById(R.id.newsFragment)
val bonusFragment = childFragmentManager.findFragmentById(R.id.bonusFragment)
val notificationsFragment = childFragmentManager.findFragmentById(R.id.notificationsFragment)
val helpFragment = childFragmentManager.findFragmentById(R.id.helpFragment)
when (currentPage) {
0 -> shoppingFragment.onOptionsItemSelected(it)
1 -> newsFragment.onOptionsItemSelected(it)
2 -> bonusFragment.onOptionsItemSelected(it)
3 -> helpFragment.onOptionsItemSelected(it)
4 -> notificationsFragment.onOptionsItemSelected(it)
else -> false
}
}
return view
}
fun showBonusesPage() {
view?.flipper?.displayedChild = 2
view?.bottomNavigation?.selectedItemId = R.id.bonus
}
fun showNewsPage() {
view?.flipper?.displayedChild = 1
view?.bottomNavigation?.selectedItemId = R.id.news
System.out.println("HEloo")
}
//this method should be called in AlertBoxActivity
fun showNewsPageTime() {
var newsFragment = 1
childFragmentManager.findFragmentById(R.id.newsFragment)
InstructionFragment().show(childFragmentManager)
}
private fun BottomNavigationView.initMenu() {
menu.clear()
menu.add(0, R.id.main, 0, R.string.main).icon = AppCompatResources.getDrawable(context, R.drawable.ic_shopping)
menu.add(0, R.id.news, 0, R.string.news).icon = AppCompatResources.getDrawable(context, R.drawable.ic_news)
menu.add(0, R.id.bonus, 0, R.string.bonus).icon = AppCompatResources.getDrawable(context, R.drawable.ic_bonus)
menu.add(0, R.id.help, 0, R.string.help).icon = AppCompatResources.getDrawable(context, R.drawable.ic_help)
menu.add(0, R.id.notifications, 0, R.string.notifications).icon = AppCompatResources.getDrawable(context, R.drawable.ic_notification)
bind(model.unreadCount) { setBadge(R.id.notifications, if (it > 0) it.toString() else null) }
}
override fun onDrawerStateChanged(newState: Int) {
}
override fun onDrawerSlide(drawerView: View, slideOffset: Float) {
}
override fun onDrawerClosed(drawerView: View) {
try {
childFragmentManager.findFragmentById(R.id.fragment_navigation_main)?.userVisibleHint = false
} catch (_: IllegalStateException) {
}
}
override fun onDrawerOpened(drawerView: View) {
try {
childFragmentManager.findFragmentById(R.id.fragment_navigation_main)?.userVisibleHint = true
} catch (_: java.lang.IllegalStateException) {
}
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
updateTitle()
}
private fun updateTitle() {
when (view?.flipper?.displayedChild) {
0 -> view?.toolbar?.setTitle(R.string.your_bright_shopping)
1 -> view?.toolbar?.setTitle(R.string.news)
2 -> view?.toolbar?.setTitle(R.string.bonus)
3 -> view?.toolbar?.setTitle(R.string.help)
4 -> view?.toolbar?.setTitle(R.string.notifications)
}
}
@SuppressLint("RestrictedApi")
private fun notifyFragments() {
val currentPage = view?.flipper?.displayedChild ?: return
val shoppingFragment = childFragmentManager.findFragmentById(R.id.shoppingFragment)
val newsFragment = childFragmentManager.findFragmentById(R.id.newsFragment)
val bonusFragment = childFragmentManager.findFragmentById(R.id.bonusFragment)
val notificationsFragment = childFragmentManager.findFragmentById(R.id.notificationsFragment)
val helpFragment = childFragmentManager.findFragmentById(R.id.helpFragment)
shoppingFragment?.userVisibleHint = currentPage == 0
newsFragment?.userVisibleHint = currentPage == 1
bonusFragment?.userVisibleHint = currentPage == 2
helpFragment?.userVisibleHint = currentPage == 3
notificationsFragment?.userVisibleHint = currentPage == 4
val menu = view?.toolbar?.menu ?: return
val inflater = SupportMenuInflater(view?.toolbar?.context)
menu.clear()
when (currentPage) {
0 -> shoppingFragment.onCreateOptionsMenu(menu, inflater)
1 -> newsFragment.onCreateOptionsMenu(menu, inflater)
2 -> bonusFragment.onCreateOptionsMenu(menu, inflater)
3 -> helpFragment.onCreateOptionsMenu(menu, inflater)
4 -> notificationsFragment.onCreateOptionsMenu(menu, inflater)
}
}
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putInt(KEY_CURRENT_ITEM, view?.flipper?.displayedChild ?: 0)
}
override fun onBackPressed(): Boolean {
val drawer = view?.drawer ?: return false
return when {
drawer.isDrawerOpen(Gravity.START) -> {
drawer.closeDrawer(Gravity.START)
true
}
view?.bottomNavigation?.selectedItemId == R.id.main -> false
else -> {
view?.bottomNavigation?.selectedItemId = R.id.main
true
}
}
}
fun onBackPressedNews(): Boolean {
view?.bottomNavigation?.selectedItemId = R.id.news
return true
}
override fun onNavigationItemSelected(item: MenuItem): Boolean {
val page = when (item.itemId) {
R.id.main -> 0
R.id.news -> 1
R.id.bonus -> 2
R.id.help -> 3
R.id.notifications -> 4
else -> return false
}
view?.flipper?.displayedChild = page
updateTitle()
notifyFragments()
return true
}
companion object {
private const val KEY_CURRENT_ITEM = "current_item"
}
}
AlertBoxActivity.kt
Активность, когда я вызываю метод showNewsPageTime()
, но когда запущен android, остановлен.
package kz.app
import android.support.design.widget.BottomNavigationView
import android.annotation.SuppressLint
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.support.v4.app.FragmentManager;
import kotlinx.android.synthetic.main.fragment_main.*
import kotlinx.android.synthetic.main.fragment_parking_payment.*
import kotlinx.android.synthetic.main.fragment_shopping.*
import kz.app.api.entity.PromoAction
import kz.app.model.PromoActionViewModel
import kz.app.fragment.NewsFragment
import kz.app.fragment.PromoDetailsFragment
import kz.app.fragment.MainFragment
import kz.app.fragment.ShoppingDetailsFragment
import kz.app.model.PromoDetailsViewModel
import android.widget.FrameLayout;
import android.R
class AlertBoxActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
val fragmentManager = supportFragmentManager
super.onCreate(savedInstanceState)
val obj = MainFragment()
obj.showNewsPageTime()
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(android.R.id.content, obj)
.commit()