Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

116 changes: 116 additions & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/jarRepositories.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
plugins {
id 'com.android.application'
}
apply plugin: 'kotlin-android'

android {
compileSdkVersion 30
Expand Down Expand Up @@ -35,4 +36,10 @@ dependencies {
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
implementation "androidx.core:core-ktx:+"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ты используешь что-то из этого пакета?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:+ лучше избегать
стоит выбрать самую послденюю актуальную версию и далее обновлять мануально. Всё новое может стать в какой-то момент слишком новым и несовместимым.

implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"

}
repositories {
mavenCentral()
}
4 changes: 2 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.KotlinHomeWork">
<activity android:name=".MainActivity">
<activity android:name=".MainActivity2">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SecondActivity"
android:name=".SecondActivity2"
android:exported="false"
android:windowSoftInputMode="adjustResize" />
</application>
Expand Down
69 changes: 0 additions & 69 deletions app/src/main/java/com/example/kotlinhomework/MainActivity.java

This file was deleted.

62 changes: 62 additions & 0 deletions app/src/main/java/com/example/kotlinhomework/MainActivity2.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.example.kotlinhomework

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity


class MainActivity2 : AppCompatActivity() {

private lateinit var greetings: String

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lateinit - хорошо!
Здесь можно сделать ещё лучше, если написать
private val greetings: String by lazy { getString(R.string.hello) }
тогда greeting проинициализируется при первом обращении, которое случится, когда context уже будет.

Очень полезный делегат, часто используется

private lateinit var name: String
private lateinit var textView: TextView

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

greetings = getString(R.string.hello)
name = if(savedInstanceState?.getString(NAME_KEY) != null)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

вот так посимпатичнее:
name = savedInstanceState?.getString(NAME_KEY) ?: getString(R.string.anon)

savedInstanceState.getString(NAME_KEY)!!
else getString(R.string.anon)

textView = findViewById(R.id.textViewHello)
val button: Button = findViewById(R.id.buttonNameYourSelf)

button.setOnClickListener {
val intent = Intent(this@MainActivity2, SecondActivity2::class.java)
startActivityForResult(intent, SecondActivity2.GET_NAME_REQUEST_CODE)
}
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)

if (requestCode == SecondActivity2.GET_NAME_REQUEST_CODE && resultCode == Activity.RESULT_OK && data != null){
val nameFromData = data.getStringExtra(SecondActivity2.NAME_KEY)
if(nameFromData != null)
name = nameFromData
}

}

@SuppressLint("SetTextI18n")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

лучше всё-таки послушаться компилятора и оставить String.format("%s, %s!", greetings, name)

override fun onResume() {
super.onResume()
textView.text = "$greetings, $name!"

}

override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putString(NAME_KEY, name)
}

companion object{
const val NAME_KEY: String = "com.example.kotlinhomework.MainActivity.NAME_KEY"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

private потерялся private

}
}
57 changes: 0 additions & 57 deletions app/src/main/java/com/example/kotlinhomework/SecondActivity.java

This file was deleted.

Loading