У меня вопрос, код kotlin ниже определяет, что BottomNavigationView виден только в activity_main, а не во фрагментах, присутствующих в нижней части навигации, суть в том, что нижняя панель видна только на первой странице, а не в остальных фрагментах, что это из-за? что мне нужно сделать, чтобы меню отображалось во всех пользовательских интерфейсах
MainActivity.kt
class MainActivity : AppCompatActivity() {
private val bottleFragment = BottleFragment()
private val homeFragment = HomeFragment()
private val profileInfoFragment = ProfileInfoFragment()
private val fragmentHistory = mutableListOf<Fragment>(homeFragment)
private var activeFragment: Fragment = homeFragment
var ID_AUTH_FIREBASE = ""
var ADMIN_KEY = ""
var email = ""
val bundle = Bundle()
private val bottomNavigationListener = BottomNavigationView.OnNavigationItemSelectedListener {
when (it.itemId) {
R.id.navigation_home -> {
setFragment(homeFragment)
return@OnNavigationItemSelectedListener true
}
R.id.navigation_profile -> {
setFragment(profileInfoFragment)
return@OnNavigationItemSelectedListener true
}
R.id.navigation_bottle -> {
setFragment(bottleFragment)
return@OnNavigationItemSelectedListener true
}
else -> {
return@OnNavigationItemSelectedListener false
}
}
}
@SuppressLint("MissingPermission")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
navigation.setOnNavigationItemSelectedListener(bottomNavigationListener)
startFragment(homeFragment)
checkPermissions()
buttonlogout.setOnClickListener { logout() }
buttonsave.setOnClickListener { save() }
window.setFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN
);
email = intent.getStringExtra("Email")
ID_AUTH_FIREBASE = intent.getStringExtra("ID_AUTH_FIREBASE")
ADMIN_KEY = intent.getStringExtra("ADMIN_KEY")
val sharedPreference = getSharedPreferences("PREFERENCE_NAME", 0)
var editor = sharedPreference.edit()
editor.putString("Email", email)
editor.putString("ID_AUTH_FIREBASE", ID_AUTH_FIREBASE)
editor.putString("ADMIN_KEY", ADMIN_KEY)
editor.commit()
val datiPersona = APISupport.getDatiPersona(ID_AUTH_FIREBASE, email, ADMIN_KEY)
textNome.setText(datiPersona[0])
txtCognome.setText(datiPersona[1])
txtResidenza.setText(datiPersona[2])
txtTelefono.setText(datiPersona[3])
txtEmail.setText(email)
hometext.text = datiPersona[0] + " " + datiPersona[1]
txtcfdesc.setText(datiPersona[5])
txtnumeropolizaedit.setText(datiPersona[6])
try {
val url: URL = URL(APISupport.geturlLogo(ID_AUTH_FIREBASE, email, ADMIN_KEY))
val bmp: Bitmap = BitmapFactory.decodeStream(url.openConnection().getInputStream())
imageLogo.setImageBitmap(bmp)
} catch (e: Exception) {
System.out.println("Errore")
}
txtDataFineEdit.setText(datiPersona[8])
txtDataInizioEdit.setText(datiPersona[7])
}
override fun onResume() {
super.onResume()
checkPermissions()
}
override fun onPause() {
super.onPause()
checkPermissions()
}
fun save() {
var value = APISupport.savedatipersona(
ID_AUTH_FIREBASE,
email,
ADMIN_KEY,
txtCognome.text.toString(),
textNome.text.toString(),
txtTelefono.text.toString(),
txtResidenza.text.toString(),
txtDataInizioEdit.text.toString(),
txtDataFineEdit.text.toString(),
txtnumeropolizaedit.text.toString(),
txtcfdesc.text.toString()
)
}
fun logout() {
val intent = Intent(this, SplashActivity::class.java)
startActivity(intent)
}
fun startFragment(fragment: Fragment, removeLast: Boolean = false) {
if (fragment == homeFragment)
startHomeBackground()
else
stopHomeBackground()
if (removeLast)
fragmentHistory.removeAt(fragmentHistory.lastIndex)
if (fragmentHistory.last() != fragment)
fragmentHistory.add(fragment)
activeFragment = fragment
}
protected fun setFragment(fragment: Fragment?) {
if (fragment == homeFragment)
startHomeBackground()
else
stopHomeBackground()
val fragmentManager = supportFragmentManager
val fragmentTransaction: FragmentTransaction = fragmentManager.beginTransaction()
if (fragment != null) {
fragmentTransaction.replace(android.R.id.content, fragment)
}
fragmentTransaction.commit()
}
fun startHomeBackground() {
homeBackground.visibility = View.VISIBLE
}
fun stopHomeBackground() {
homeBackground.visibility = View.GONE
}
private fun checkPermissions() {
if (ActivityCompat.checkSelfPermission(
this,
Manifest.permission.ACCESS_FINE_LOCATION
) != PackageManager.PERMISSION_GRANTED
) {
requestPermissions(
arrayOf(Manifest.permission.ACCESS_FINE_LOCATION),
RequestCodes.PERMISSION_GPS
)
return
}
if (ActivityCompat.checkSelfPermission(
this,
Manifest.permission.BLUETOOTH
) != PackageManager.PERMISSION_GRANTED
) {
requestPermissions(
arrayOf(Manifest.permission.BLUETOOTH),
RequestCodes.PERMISSION_BLUETOOTH
)
return
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
if (ActivityCompat.checkSelfPermission(
this,
Manifest.permission.FOREGROUND_SERVICE
) != PackageManager.PERMISSION_GRANTED
) {
requestPermissions(
arrayOf(Manifest.permission.FOREGROUND_SERVICE),
RequestCodes.PERMISSION_FOREGROUND_SERVICE
)
return
}
}
}
private fun Fragment.isMainFragment(): Boolean {
when (this) {
homeFragment, bottleFragment, profileInfoFragment -> {
return true
}
}
return false
}
override fun onBackPressed() {
if (fragmentHistory.last().isMainFragment()) {
finish()
return
}
val lastIndex = fragmentHistory.lastIndex
try {
startFragment(fragmentHistory[lastIndex - 1], true)
} catch (e: IndexOutOfBoundsException) {
e.printStackTrace()
finish()
}
}
}
activity_main.xml
....
<!-- Bottom Navigation View -->
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="0dp"
android:layout_height="85dp"
android:background="#FFFAFA"
android:backgroundTint="@android:color/transparent"
app:itemBackground="@android:color/transparent"
app:itemIconSize="28dp"
app:itemIconTint="@color/bottom_navigation_color"
app:labelVisibilityMode="unlabeled"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/bottom_navigation" />
..
bottom_navigation.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/navigation_home"
android:visible="true"
android:icon="@drawable/profile_nav_selector"
android:tooltipText="Profilo"
android:enabled="true"
android:title="Profilo" />
<item
android:id="@+id/navigation_profile"
android:visible="true"
android:icon="@drawable/home_nav_selector"
android:tooltipText="Veicoli"
android:enabled="true"
android:title="Veicoli" />
<item
android:id="@+id/navigation_bottle"
android:icon="@drawable/bottle_nav_selector"
android:tooltipText="Info"
android:visible="true"
android:enabled="true"
android:title="Info" />
</menu>
fragment_bottle.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/alertWhite"
tools:context=".fragments.BottleFragment">
<!-- Home Background -->
<FrameLayout
android:layout_width="0dp"
android:id="@+id/homeBackground"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="140dp"
android:background="@drawable/oval"
android:baselineAligned="false"
android:layout_marginTop="-70dp"
android:orientation="vertical"
tools:ignore="MissingConstraints">
<TextView
android:id="@+id/hometext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="80dp"
android:layout_marginLeft="-10dp"
android:text="Informazioni"
android:textSize="20dp" />
</LinearLayout>
<ImageView
android:layout_width="100dp"
android:layout_height="80dp"
android:layout_marginTop="70dp"
android:layout_marginLeft="130dp"
android:background="@drawable/infobutton"
tools:ignore="MissingConstraints" />
<Button
android:id="@+id/buttonInfoPrivacy"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_marginTop="150dp"
android:background="@drawable/roundbutton"
android:layout_marginLeft="60dp"
android:radius="14dp"
android:text="Infomativa Privacy"
android:textColor="#FFFFFF" />
<Button
android:id="@+id/buttontermini"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_marginTop="230dp"
android:background="@drawable/roundbutton"
android:layout_marginLeft="60dp"
android:radius="14dp"
android:text="Termini e Condizioni"
android:textColor="#FFFFFF" />
<ImageView
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_marginTop="280dp"
android:layout_marginLeft="100dp"
android:background="@drawable/logolive"
tools:ignore="MissingConstraints" />
<TextView
android:id="@+id/txtnumber"
android:layout_width="100dp"
android:layout_height="30dp"
android:layout_alignLeft="@+id/textview"
android:layout_alignParentBottom="true"
android:layout_marginTop="430dp"
android:textColor="#110F0F"
android:layout_marginLeft="155dp"
android:textSize="15dp"
android:textStyle="bold"
android:text="v. 1.0" />
</FrameLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
fragment_profile_info.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/alertWhite"
tools:context=".fragments.ProfileInfoFragment">
<FrameLayout
android:id="@+id/frameProfile"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:ignore="MissingConstraints">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="140dp"
android:background="@drawable/oval"
android:baselineAligned="false"
android:layout_marginTop="-70dp"
android:orientation="vertical"
tools:ignore="MissingConstraints">
<ImageView
android:id="@+id/buttonlogout"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginTop="80dp"
android:layout_marginLeft="50dp"
android:background="@drawable/iconback" />
<TextView
android:id="@+id/hometext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="-30dp"
android:layout_marginLeft="20dp"
android:text="I tuoi veicoli"
android:textSize="20dp" />
<ImageView
android:id="@+id/buttonadd"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginTop="-26dp"
android:layout_marginLeft="270dp"
android:background="@drawable/iconadd" />
<ListView
android:id="@+id/list"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:divider="#000"
android:layout_marginTop="100dp"
android:dividerHeight="1dp"
android:footerDividersEnabled="false"
android:headerDividersEnabled="false" />
</LinearLayout>
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</FrameLayout>
</androidx.constraintlayout.widget.ConstraintLayout>