Im new to kotlin and Im trying to make a navigation bar with a time selector based on NumberPicker, with 2 other empty fragments (for now atleast), but my problem is that if I start an intent the Navigation bar doesnt show, when I dont put it in code (intent) the navigation bar shows up working correctly, what Im doing wrong, am I missing something??
MainActivity.kt
package com.example.helloworld
import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import com.example.helloworld.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.bottomNavigationView.setOnItemSelectedListener {
when (it.itemId) {
R.id.first -> replaceFragment(Home())
R.id.second -> replaceFragment(Voices())
R.id.third -> replaceFragment(Settings())
else -> {
}
}
true
}
replaceFragment(Home())
Intent(this, SecondActivity::class.java).also {
startActivity(it)
}
}
private fun replaceFragment(fragment: Fragment) {
val fragmentManager = supportFragmentManager
val fragmentTransaction = fragmentManager.beginTransaction()
fragmentTransaction.replace(R.id.frameLayout, fragment)
fragmentTransaction.commit()
}
}
SecondActivity.kt
package com.example.helloworld
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_second.*
class SecondActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_second)
numPickerMin2.minValue = 0
numPickerMin2.maxValue = 23
numPickerSec2.minValue = 0
numPickerSec2.maxValue = 59
var min = 0
var sec = 0
var amOrPm = ""
val str = arrayOf<String>("AM", "PM")
numPickerAM2.minValue = 0
numPickerAM2.maxValue = (str.size - 1)
numPickerAM2.displayedValues = str
numPickerMin2.setOnValueChangedListener { numberPicker, i, i2 ->
min = numberPicker.value
}
numPickerSec2.setOnValueChangedListener { numberPicker, i, i2 ->
sec = numberPicker.value
}
numPickerAM2.setOnValueChangedListener { numberPicker, i, i2 ->
val i = numberPicker.value
amOrPm = str[i]
}
btGetValue2.setOnClickListener {
reminderTime2.text = "$min : $sec : $amOrPm"
}
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.helloworld"
android:installLocation="preferExternal"
android:versionCode="1"
android:versionName="0.2">
<uses-sdk
android:minSdkVersion="18"
android:targetSdkVersion="27" />
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
tools:targetApi="31"
android:theme="@style/Theme.HelloWorld">
<activity
android:name=".MainActivity"
android:exported="true"
android:screenOrientation="sensorPortrait"
android:label="@string/app_name"
android:launchMode="singleInstance">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SecondActivity">
</activity>
</application>
</manifest>
ActivityMain.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"
tools:context=".MainActivity">
<FrameLayout
android:id="@+id/frameLayout"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@id/bottomNavigationView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</FrameLayout>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottomNavigationView"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="@menu/bottom_nav"/>
</androidx.constraintlayout.widget.ConstraintLayout>
activity_second.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:layout_marginStart="1dp"
android:layout_marginTop="1dp"
android:layout_marginEnd="1dp"
android:layout_marginBottom="1dp"
android:background="@color/BackgroundFirst"
tools:context=".Home">
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/textInputLayout4"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense.ExposedDropdownMenu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:layout_marginTop="55dp"
app:layout_constraintEnd_toStartOf="@+id/guideline2"
app:layout_constraintStart_toStartOf="@+id/guideline2"
app:layout_constraintTop_toTopOf="@+id/guideline3">
<AutoCompleteTextView
android:id="@+id/dropdown_field"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="none"
android:text="Choose Alert"/>
</com.google.android.material.textfield.TextInputLayout>
<com.shawnlin.numberpicker.NumberPicker
android:id="@+id/numPickerMin2"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_centerInParent="true"
android:layout_marginEnd="23dp"
android:gravity="center"
android:theme="@style/AppTheme.Picker"
app:layout_constraintBottom_toBottomOf="@+id/numPickerSec2"
app:layout_constraintEnd_toStartOf="@+id/numPickerSec2"
app:layout_constraintTop_toTopOf="@+id/numPickerSec2"
app:np_dividerColor="@color/black"
app:np_dividerDistance="55dp"
app:np_dividerLength="100dp"
app:np_dividerThickness="3dp"
app:np_formatter=""
app:np_height="180dp"
app:np_max="12"
app:np_min="0"
app:np_selectedTextColor="@color/selected"
app:np_selectedTextSize="60sp"
app:np_textAlign="textAlignCenter"
app:np_textColor="@color/black"
app:np_textSize="40sp"
app:np_typeface="@string/roboto_light"
app:np_value="1"
app:np_wheelItemCount="10"
app:np_width="74dp"
app:np_wrapSelectorWheel="true" />
<com.shawnlin.numberpicker.NumberPicker
android:id="@+id/numPickerSec2"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_centerInParent="true"
android:layout_marginTop="37dp"
android:layout_marginEnd="9dp"
android:layout_marginBottom="27dp"
android:gravity="center"
android:theme="@style/AppTheme.Picker"
app:layout_constraintBottom_toTopOf="@+id/textInputLayout4"
app:layout_constraintEnd_toStartOf="@+id/numPickerAM2"
app:layout_constraintTop_toBottomOf="@+id/imageView2"
app:np_dividerColor="@color/black"
app:np_dividerDistance="55dp"
app:np_dividerLength="100dp"
app:np_dividerThickness="3dp"
app:np_formatter=""
app:np_height="180dp"
app:np_max="59"
app:np_min="0"
app:np_selectedTextColor="@color/selected"
app:np_selectedTextSize="60sp"
app:np_textAlign="textAlignCenter"
app:np_textColor="@color/black"
app:np_textSize="40sp"
app:np_typeface="@string/roboto_light"
app:np_value="1"
app:np_wheelItemCount="10"
app:np_width="74dp"
app:np_wrapSelectorWheel="true" />
<com.shawnlin.numberpicker.NumberPicker
android:id="@+id/numPickerAM2"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_centerInParent="true"
android:layout_marginStart="56dp"
android:layout_marginEnd="55dp"
android:gravity="center"
android:theme="@style/AppTheme.Picker"
app:layout_constraintBottom_toBottomOf="@+id/numPickerSec2"
app:layout_constraintEnd_toEndOf="@+id/imageView2"
app:layout_constraintStart_toStartOf="@+id/guideline2"
app:layout_constraintTop_toTopOf="@+id/numPickerSec2"
app:np_dividerColor="@color/black"
app:np_dividerDistance="55dp"
app:np_dividerLength="100dp"
app:np_dividerThickness="3dp"
app:np_formatter=""
app:np_height="180dp"
app:np_max="1"
app:np_min="0"
app:np_selectedTextColor="@color/selected"
app:np_selectedTextSize="60sp"
app:np_textAlign="textAlignCenter"
app:np_textColor="@color/black"
app:np_textSize="40sp"
app:np_typeface="@string/roboto_light"
app:np_value="1"
app:np_wheelItemCount="10"
app:np_width="74dp"
app:np_wrapSelectorWheel="true" />
<Button
android:id="@+id/btGetValue2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="78dp"
android:backgroundTint="@color/selected"
android:baselineAligned="false"
android:text="Get Time"
android:textSize="15sp"
app:layout_constraintBottom_toTopOf="@+id/reminderTime2"
app:layout_constraintEnd_toStartOf="@+id/guideline2"
app:layout_constraintStart_toStartOf="@+id/guideline2" />
<TextView
android:id="@+id/reminderTime2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="63dp"
android:text="Time"
android:textSize="40sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/guideline2"
app:layout_constraintStart_toStartOf="@+id/guideline2" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="351dp"
android:layout_height="103dp"
android:layout_marginTop="16dp"
android:layout_weight="1"
android:scaleX="1.5"
android:scaleY="1.5"
app:layout_constraintEnd_toStartOf="@+id/guideline2"
app:layout_constraintStart_toStartOf="@+id/guideline2"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/helloday_logo" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.5"
/>
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.5" />
</androidx.constraintlayout.widget.ConstraintLayout>
Instead of creating a second activity, I suggest that you create another fragment. Use it in the same way as the other fragments which you currently have in your app.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.