티스토리 뷰
build.gradle (Module: app)
dependencies {
// Coroutine
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.7'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.4'
}
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>
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(applicationContext, MainActivity::class.java))
finish()
}
}
}
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"
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>
MainActivity.kt
package com.jwsoft.kotlinproject
import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModelProvider
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
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)
btnDownload.setOnClickListener {
myViewModel.downloadData(applicationContext)
}
btnMain.setOnClickListener {
startActivity(Intent(applicationContext, SubActivity::class.java))
finish()
}
}
}
MyViewModel.kt
package com.jwsoft.kotlinproject
import android.content.Context
import android.widget.Toast
import androidx.lifecycle.ViewModel
import kotlinx.coroutines.*
class MyViewModel : ViewModel() {
private val job = Job()
private val downloadScope = CoroutineScope(Dispatchers.Main + job)
fun downloadData(context: Context) {
downloadScope.launch {
repeat(100) {
Toast.makeText(context, "메인화면에 필요한 데이터 받는 중... ${it+1}%", Toast.LENGTH_SHORT).show()
delay(2000)
}
}
}
override fun onCleared() {
super.onCleared()
job.cancel()
}
}
'Android > Kotlin' 카테고리의 다른 글
[Kotlin] MVVM + Coroutine (0) | 2020.07.30 |
---|---|
[Kotlin] Coroutine + CoroutineExceptionHandler (0) | 2020.07.29 |
[Kotlin] Coroutine (2) (0) | 2020.07.29 |
[Kotlin] Thread.join() (0) | 2020.07.27 |
[Kotlin] Synchronized (0) | 2020.07.27 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- Intent
- ViewPager2
- Architecture Pattern
- 혀가 길지 않은 개발자
- JSONArray
- Design Pattern
- James Kim
- DataBinding
- XML
- fragment
- JSONObject
- activity
- ViewModel
- MVVM
- Kotlin
- coroutine
- recyclerview
- ArrayList
- Vue.js #Vue.js + javascript
- CoordinatorLayout
- 자바
- 안드로이드 #코틀린 #Android #Kotlin
- java
- handler
- View
- 코틀린
- TabLayout
- 안드로이드
- Android
- Livedata
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 31 |
글 보관함