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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ run.sh
*.aab
output-metadata.json
/build.properties
*.hprof
*.hprof
6 changes: 0 additions & 6 deletions README.md

This file was deleted.

3 changes: 2 additions & 1 deletion app/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/build
/build
/.gradle/
7 changes: 6 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id 'kotlin-parcelize'
}

android {
Expand Down Expand Up @@ -35,4 +36,8 @@ dependencies {
implementation 'androidx.core:core-ktx:1.8.0'
implementation 'androidx.appcompat:appcompat:1.5.0'
implementation 'com.google.android.material:material:1.6.1'
}
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package ru.otus.otusdemo

import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4

import org.junit.Test
import org.junit.runner.RunWith

import org.junit.Assert.*

/**
* Instrumented test, which will execute on an Android device.
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
@Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("ru.otus.otusdemo", appContext.packageName)
}
}
41 changes: 37 additions & 4 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,17 +1,50 @@
<?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="otus.gpb.homework.activities">
package="ru.otus.otusdemo">

<application
android:name=".OtusApplication"
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"
android:theme="@style/Theme.Activities"
tools:targetApi="31" />
android:theme="@style/Theme.OtusDemo"
tools:targetApi="31">
<activity
android:name=".ActivityD"
android:exported="false" />
<activity
android:name=".ActivityC"
android:exported="false" />
<activity
android:name=".ActivityB"
android:launchMode="standard"
android:taskAffinity=""
android:exported="false" />
<activity
android:name=".ActivityA"
android:launchMode="singleTask"
android:taskAffinity=""
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

</manifest>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:exported="true"
android:launchMode="standard">
</activity>
<activity
android:name=".SecondActivity"
android:exported="false"
android:launchMode="singleTask" />
</application>

</manifest>
28 changes: 28 additions & 0 deletions app/src/main/java/ru/otus/otusdemo/ActivityA.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package ru.otus.otusdemo

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.Toast

class ActivityA : AppCompatActivity() {

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

val button = findViewById<Button>(R.id.act_a_button_open_to_act_B)
button.setOnClickListener {
val intent = Intent(applicationContext, ActivityB::class.java)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

@DariaMandzyuk , так не получится открыть новый таск.
Нужно или в манифесте установить таск аффинити отличный от А или использовать флаги:
Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_MULTIPLE_TASK

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
startActivity(intent)
}
}

override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
Toast.makeText(this, "мы попали на существующий экземпляр ActivityA", Toast.LENGTH_SHORT)
.show()
}
}
26 changes: 26 additions & 0 deletions app/src/main/java/ru/otus/otusdemo/ActivityB.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package ru.otus.otusdemo

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.Toast

@Suppress("DEPRECATION")
class ActivityB : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_b)
val button = findViewById<Button>(R.id.act_b_button_open_to_act_C)

button.setOnClickListener {
val intent = Intent(applicationContext, ActivityC::class.java)
startActivity(intent)
}
}

override fun onBackPressed() {
super.onBackPressed()
Toast.makeText(this, "Кнопка бэк была нажата", Toast.LENGTH_SHORT).show()
}
}
50 changes: 50 additions & 0 deletions app/src/main/java/ru/otus/otusdemo/ActivityC.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package ru.otus.otusdemo

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.Toast

@Suppress("DEPRECATION")
class ActivityC : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_c)

val button1 = findViewById<Button>(R.id.act_c_button_to_act_a)
button1.setOnClickListener {
val intent = Intent(applicationContext, ActivityA::class.java)
startActivity(intent)
}

val button2 = findViewById<Button>(R.id.act_c_button_to_act_d)
button2.setOnClickListener {
val intent = Intent(applicationContext, ActivityD::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
startActivity(intent)
}

val button3 =
findViewById<Button>(R.id.act_c_close_act_c) // По клику на кнопку “CloseActivityC”, завершите ActivityC, и перейдите на предыдущий экран в стеке
button3.setOnClickListener {
finish()
// val intent = Intent(applicationContext, ActivityB::class.java)
// startActivity(intent)
}

val button4 =
findViewById<Button>(R.id.act_c_close_stack) // По клику на кнопку “Close Stack” завершите текущий стек, в котором находятся ActivityB и ActivityC, и перейдите на ActivityA
button4.setOnClickListener {
finishAffinity()
val intent = Intent(applicationContext, ActivityA::class.java)
startActivity(intent)
}
}

override fun onBackPressed() {
super.onBackPressed()
Toast.makeText(this, "Кнопка бэк была нажата", Toast.LENGTH_SHORT).show()
}
}
30 changes: 30 additions & 0 deletions app/src/main/java/ru/otus/otusdemo/ActivityD.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package ru.otus.otusdemo

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.Toast

@Suppress("DEPRECATION")
class ActivityD : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_d)

val button1 = findViewById<Button>(R.id.act_d_button)
button1.setOnClickListener {
Toast.makeText(this, "ТОЧНО END!", Toast.LENGTH_SHORT).show()
}
}

override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
Toast.makeText(this, "onNewIntent", Toast.LENGTH_SHORT).show()
}

override fun onBackPressed() {
super.onBackPressed()
Toast.makeText(this, "Кнопка бэк была нажата", Toast.LENGTH_SHORT).show()
}
}
53 changes: 53 additions & 0 deletions app/src/main/java/ru/otus/otusdemo/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package ru.otus.otusdemo

import android.content.Intent
import android.os.Bundle
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {

// вызывается в момент инициализиации экрана
// и служит для задания системных параметров, файла верстки
// Вызывается перед отрисовкой экрана, верстка создана, но экран не отобразился
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}

// Вызывается после отрисовки экрана, но пользователь в момент вызова еще не может
// взаимодействовать с интерфейсом
override fun onStart() {
super.onStart()
val button = findViewById<Button>(R.id.buttonSecondActivity)
button.setOnClickListener {

// val id = (applicationContext as OtusApplication).id
// Toast.makeText(this, "Кнопка бэк была нажата", Toast.LENGTH_SHORT).show()

val intent = Intent(applicationContext, SecondActivity::class.java)
startActivity(intent)
}
}

// Весь экран отрисован, готов к работе, пользователю доступно взаимодействие с интерфейсом
override fun onResume() {
super.onResume()
}

//У пользователя пропадает возможность взаимодействовать с интерфейсом, хотя экран еще виден
override fun onPause() {
super.onPause()
}

// Экран уже не виден
override fun onStop() {
super.onStop()
// отменить все запросы кеоторые были отправлены но не дошли
}

// Наша активити уничтожена и больше нет ее в памяти
override fun onDestroy() {
super.onDestroy()
}
}
14 changes: 14 additions & 0 deletions app/src/main/java/ru/otus/otusdemo/OtusApplication.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package ru.otus.otusdemo

import android.app.Application

class OtusApplication : Application() {


val id = "Im from Application"

override fun onCreate() {
super.onCreate()

}
}
30 changes: 30 additions & 0 deletions app/src/main/java/ru/otus/otusdemo/SecondActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package ru.otus.otusdemo

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.Toast

class SecondActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_second)
val button = findViewById<Button>(R.id.secondActivityButton)
button.setOnClickListener {
val intent = Intent(applicationContext, SecondActivity::class.java)
startActivity(intent)
}
}

override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
Toast.makeText(this, "onNewIntent", Toast.LENGTH_SHORT).show()
}

override fun onBackPressed() {
super.onBackPressed()
Toast.makeText(this, "Кнопка бэк была нажата", Toast.LENGTH_SHORT).show()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package ru.otus.otusdemo.examples

import android.app.Activity
import android.content.Context
import android.content.Intent
import androidx.activity.result.contract.ActivityResultContract
import ru.otus.otusdemo.MainActivity

class ContractMainActivityAndSecondActivity : ActivityResultContract<String, String?>() {

override fun createIntent(context: Context, input: String): Intent {
//return Intent(context, SecondActivity::class.java).apply {
return Intent(context, MainActivity::class.java).apply {
putExtra("currentName", input)
}
}

override fun parseResult(resultCode: Int, intent: Intent?): String? {
if (intent == null) return null
if (resultCode != Activity.RESULT_OK) return null

return intent.getStringExtra("resultKey")
}
}
Loading