Я добавил меню в свое приложение. В этом меню у меня есть пункт, который позволяет пользователю воспроизводить это вступление, которое воспроизводится в первый раз, когда пользователь открывает приложение после его установки. Тем не менее, я использую Shared Preferences, чтобы увидеть, является ли пользователь впервые или нет. Я не уверен, как это сделать. Вот мой код:
Вступительный класс:
class Intro : AppCompatActivity() {
private val introSliderAdapter = IntroSliderAdapter(
listOf(
IntroSlide("Capture a ignição",
"Tire uma fotografia á ignição que avista. Por favor mantanha o telemóvel na vertical",
R.drawable.image1),
IntroSlide("Localização",
"É necessário que tenha o GPS ligado do seu telémovel. Ao capturar a fotografia serão automáticamente gravados a sua localização e o ângulo a que o fogo se encontra",
R.drawable.image2),
IntroSlide("Envio para os operacionais ",
"Após os passos anteriormente devidamente cumpridos, a informação fornecida será devidamente avaliada por operacionais responsáveis.",
R.drawable.image3)
)
)
private var PRIVATE_MODE = 0
private val PREF_NAME = "mindorks-welcome"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
//setContentView(R.layout.activity_main)
val sharedPref: SharedPreferences = getSharedPreferences(PREF_NAME, PRIVATE_MODE)
if (sharedPref.getBoolean(PREF_NAME, false)) {
val homeIntent = Intent(this, HomePage::class.java)
startActivity(homeIntent)
finish()
} else {
setContentView(R.layout.activity_intro)
playIntro()
val editor = sharedPref.edit()
editor.putBoolean(PREF_NAME, true)
editor.apply()
}
}
fun playIntro()
{
introSliderViewPager.adapter=introSliderAdapter
setupIndicators()
setCurrentIndicator(0)
introSliderViewPager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback(){
override fun onPageSelected(position: Int) {
setCurrentIndicator(position)
}
})
buttonNext.setOnClickListener {
if(introSliderViewPager.currentItem+1 < introSliderAdapter.itemCount){
introSliderViewPager.currentItem+=1
}else{
Intent(applicationContext, HomePage::class.java).also {
startActivity(it)
finish()
}
}
}
buttonSkip.setOnClickListener {
Intent(applicationContext, HomePage::class.java).also {
startActivity(it)
finish()
}
}
}
private fun setupIndicators(){
val indicators = arrayOfNulls<ImageView>(introSliderAdapter.itemCount)
val layoutParams : LinearLayout.LayoutParams = LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
)
layoutParams.setMargins(8,0,8,0)
for (i in indicators.indices){
indicators[i] = ImageView(applicationContext)
indicators[i].apply {
this?.setImageDrawable(
ContextCompat.getDrawable(
applicationContext,
R.drawable.indicator_inactive
)
)
this?.layoutParams = layoutParams
}
indicatorsContainer.addView(indicators[i])
}
}
private fun setCurrentIndicator(index:Int ){
val childCount = indicatorsContainer.childCount
for(i in 0 until childCount){
val imageView = indicatorsContainer[i] as ImageView
if(i==index){
imageView.setImageDrawable(
ContextCompat.getDrawable(
applicationContext,
R.drawable.indicator_active
)
)
}else{
imageView.setImageDrawable(
ContextCompat.getDrawable(
applicationContext,
R.drawable.indicator_inactive
)
)
}
}
if(index == introSliderAdapter.itemCount -1)
{
buttonNext.text ="DONE"
}
}
}
Вот моя домашняя страница, где находится меню:
class HomePage : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_homepage)
setSupportActionBar(findViewById(R.id.toolbar))
val actionBar = supportActionBar
actionBar!!.title=""
val btnCriar: Button = findViewById(R.id.criar)
btnCriar.setOnClickListener {
val intent = Intent(this, AppFogos::class.java)
startActivity(intent)
}
// getData()
}
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.menu,menu);
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
val id = item.itemId;
if (id == R.id.sobre){
Toast.makeText(this, "item Add Clicked", Toast.LENGTH_SHORT).show()
return true
}else{
if(id==R.id.intro) {
//PLAY INTRO
return true
}
}
return super.onOptionsItemSelected(item)
}
}