티스토리 뷰
build.gradle (Module: app)
dependencies {
// viewModelScope
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0'
// Coroutine
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.7'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.7'
}
activity_main.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">
<Button
android:id="@+id/btnMain"
android:layout_width="match_parent"
android:layout_height="90dp"
android:textAllCaps="false"
android:text="Main -> Sub"
android:textSize="24dp"
android:layout_marginBottom="80dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
<Button
android:id="@+id/btnDownload"
android:layout_width="match_parent"
android:layout_height="90dp"
android:textAllCaps="false"
android:text="Download"
android:textSize="24dp"
app:layout_constraintTop_toBottomOf="@+id/btnMain"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
activity_sub.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">
<Button
android:id="@+id/btnSub"
android:layout_width="match_parent"
android:layout_height="90dp"
android:textAllCaps="false"
android:text="Sub -> Main"
android:textSize="24dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.kt
package com.jwsoft.kotlinproject
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.lifecycle.ViewModelProvider
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
private lateinit var myViewModel: MyViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
myViewModel = ViewModelProvider(
this,
ViewModelProvider.NewInstanceFactory()).get(MyViewModel::class.java
)
btnMain.setOnClickListener {
startActivity(Intent(this, SubActivity::class.java))
finish()
}
btnDownload.setOnClickListener {
myViewModel.startDownload(applicationContext)
}
}
}
SubActivity.kt
package com.jwsoft.kotlinproject
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_sub.*
class SubActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_sub)
btnSub.setOnClickListener {
startActivity(Intent(this, MainActivity::class.java))
finish()
}
}
}
MyViewModel.kt
package com.jwsoft.kotlinproject
import android.content.Context
import android.widget.Toast
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.*
class MyViewModel : ViewModel() {
fun startDownload(context: Context) {
viewModelScope.launch {
repeat(100) {
var percent = "메인화면에 필요한 데이터 받는 중... ${it+1}%"
Toast.makeText(context, percent, Toast.LENGTH_SHORT).show()
delay(2500)
}
}
}
}
'Android > Kotlin' 카테고리의 다른 글
[Kotlin] NestedScrollView (0) | 2020.08.10 |
---|---|
[Kotlin] ProgressBar (0) | 2020.08.07 |
[Kotlin] by lazy (0) | 2020.08.07 |
[Kotlin] BuildConfig (0) | 2020.08.07 |
[Kotlin] enum (0) | 2020.08.07 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- Android
- Kotlin
- DataBinding
- activity
- 코틀린
- java
- MVVM
- 안드로이드 #코틀린 #Android #Kotlin
- XML
- Intent
- View
- CoordinatorLayout
- JSONObject
- Architecture Pattern
- ViewPager2
- ArrayList
- ViewModel
- 혀가 길지 않은 개발자
- Design Pattern
- Livedata
- recyclerview
- handler
- 안드로이드
- fragment
- coroutine
- James Kim
- Vue.js #Vue.js + javascript
- JSONArray
- TabLayout
- 자바
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
글 보관함