实现单个activity加载多个fragment
更轻松以及简化activity使用
思路
由另一个界面跳转到当前activity时,携带参数值用来判断本次跳转加载哪个碎片事件
实现
这里通过intent传值
1 2 3 4 5
| val intent = Intent(this@SettingActivity, FeedbackActivity::class.java) intent.putExtra("Fragment", 0) startActivity(intent) overridePendingTransition(0, 0) finish()
|
当前activity接收加载碎片事件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| private val userFeedbackFragment = UserFeedbackFragment() private val bugFeedbackFragment = BugFeedbackFragment() val intExtra = intent.getIntExtra("Fragment", 0) when (intExtra) { 0 -> { supportFragmentManager .beginTransaction() .add(R.id.frag_feed, userFeedbackFragment, null) .commit() } 1 -> { tv_feed_title.text = getString(R.string.bug_feedback) supportFragmentManager .beginTransaction() .add(R.id.frag_feed, bugFeedbackFragment, null) .commit() }
|
描述
通过intExtra接收值判断加载设定碎片的加载事件