-
Notifications
You must be signed in to change notification settings - Fork 57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Activity #1 #34
base: master
Are you sure you want to change the base?
Activity #1 #34
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package otus.gpb.homework.activities | ||
|
||
import android.content.Intent | ||
import android.os.Bundle | ||
import android.util.Log | ||
import android.widget.Button | ||
import androidx.appcompat.app.AppCompatActivity | ||
|
||
class ActivityA : AppCompatActivity() { | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
setContentView(R.layout.activity_a) | ||
|
||
val openActivityBButton = findViewById<Button>(R.id.btn_activity_a) | ||
openActivityBButton.setOnClickListener { | ||
// Обработка нажатия на кнопку "Open ActivityB" | ||
val intent = Intent(this, ActivityB::class.java) | ||
//intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK | ||
startActivity(intent) | ||
} | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @NanaOz, добавьте сюда, пожалуйста, переопределение метода Log.w("A", "New intent") Таким образом, вы сможете проверить выполнение задания 3 |
||
override fun onNewIntent(intent: Intent?) { | ||
Log.w("ActivityA", "call new intent activityA") | ||
super.onNewIntent(intent) | ||
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package otus.gpb.homework.activities | ||
|
||
import android.content.Intent | ||
import android.os.Bundle | ||
import android.widget.Button | ||
import androidx.appcompat.app.AppCompatActivity | ||
|
||
class ActivityB : AppCompatActivity() { | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
setContentView(R.layout.activity_b) | ||
|
||
val openActivityCButton = findViewById<Button>(R.id.btn_activity_b) | ||
openActivityCButton.setOnClickListener { | ||
// Обработка нажатия на кнопку "Open ActivityC" | ||
val intent = Intent(this, ActivityC::class.java) | ||
startActivity(intent) | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package otus.gpb.homework.activities | ||
|
||
import android.content.Intent | ||
import android.os.Bundle | ||
import android.widget.Button | ||
import androidx.appcompat.app.AppCompatActivity | ||
|
||
class ActivityC : AppCompatActivity() { | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
setContentView(R.layout.activity_c) | ||
|
||
val openActivityAButton = findViewById<Button>(R.id.btn_activity_c_open_a) | ||
openActivityAButton.setOnClickListener { | ||
// Обработка нажатия на кнопку "Open ActivityA" | ||
val intent = Intent(this, ActivityA::class.java) | ||
//intent.flags = Intent.FLAG_ACTIVITY_SINGLE_TOP | ||
startActivity(intent) | ||
} | ||
|
||
val openActivityDButton = findViewById<Button>(R.id.btn_activity_c_open_d) | ||
openActivityDButton.setOnClickListener { | ||
// Обработка нажатия на кнопку "Open ActivityD" | ||
|
||
val intent = Intent(this, ActivityD::class.java) | ||
intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK | ||
startActivity(intent) | ||
} | ||
|
||
val closeActivityCButton = findViewById<Button>(R.id.btn_activity_c_close_c) | ||
closeActivityCButton.setOnClickListener { | ||
// Обработка нажатия на кнопку "Close ActivityC" | ||
finish() | ||
} | ||
|
||
val closeStackButton = findViewById<Button>(R.id.btn_activity_c_close_stack) | ||
closeStackButton.setOnClickListener { | ||
// Обработка нажатия на кнопку "Close Stack" | ||
val intent = Intent(this, ActivityA::class.java) | ||
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK | ||
startActivity(intent) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @NanaOz, отлично! Это совместимый метод. Но рекомендую еще посмотреть метод finishAffinity |
||
finishAffinity() | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package otus.gpb.homework.activities | ||
|
||
import android.os.Bundle | ||
import androidx.appcompat.app.AppCompatActivity | ||
|
||
class ActivityD : AppCompatActivity() { | ||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
setContentView(R.layout.activity_d) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?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" | ||
android:background="#f44336"> | ||
|
||
<Button | ||
android:id="@+id/btn_activity_a" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:text="@string/button" | ||
app:layout_constraintBottom_toBottomOf="parent" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintTop_toTopOf="parent" | ||
tools:ignore="MissingConstraints" /> | ||
</androidx.constraintlayout.widget.ConstraintLayout> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?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" | ||
android:background="#4caf50"> | ||
|
||
<Button | ||
android:id="@+id/btn_activity_b" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:text="@string/open_activityc" | ||
app:layout_constraintBottom_toBottomOf="parent" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintHorizontal_bias="0.796" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintTop_toTopOf="parent" | ||
app:layout_constraintVertical_bias="0.691" | ||
tools:ignore="MissingConstraints" /> | ||
</androidx.constraintlayout.widget.ConstraintLayout> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?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" | ||
android:background="#2196f3"> | ||
|
||
<Button | ||
android:id="@+id/btn_activity_c_open_a" | ||
android:layout_width="176dp" | ||
android:layout_height="50dp" | ||
android:layout_marginStart="64dp" | ||
android:layout_marginTop="304dp" | ||
android:text="@string/open_activitya" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintTop_toTopOf="parent" /> | ||
|
||
<Button | ||
android:id="@+id/btn_activity_c_close_c" | ||
android:layout_width="176dp" | ||
android:layout_height="50dp" | ||
android:layout_marginStart="64dp" | ||
android:layout_marginTop="8dp" | ||
android:text="@string/close_activityc" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintTop_toBottomOf="@+id/btn_activity_c_open_d" /> | ||
|
||
<Button | ||
android:id="@+id/btn_activity_c_close_stack" | ||
android:layout_width="176dp" | ||
android:layout_height="50dp" | ||
android:layout_marginStart="64dp" | ||
android:layout_marginBottom="192dp" | ||
android:text="@string/close_stack" | ||
app:layout_constraintBottom_toBottomOf="parent" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintTop_toBottomOf="@+id/btn_activity_c_close_c" | ||
app:layout_constraintVertical_bias="0.294" /> | ||
|
||
<Button | ||
android:id="@+id/btn_activity_c_open_d" | ||
android:layout_width="176dp" | ||
android:layout_height="50dp" | ||
android:layout_marginStart="64dp" | ||
android:layout_marginTop="8dp" | ||
android:text="@string/open_activityd" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintTop_toBottomOf="@+id/btn_activity_c_open_a" /> | ||
|
||
</androidx.constraintlayout.widget.ConstraintLayout> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?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" | ||
android:background="#ffeb3b"> | ||
|
||
<TextView | ||
android:id="@+id/textView_activiti_d" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:text="@string/activity_d" | ||
tools:layout_editor_absoluteX="73dp" | ||
tools:layout_editor_absoluteY="240dp" /> | ||
</androidx.constraintlayout.widget.ConstraintLayout> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,10 @@ | ||
<resources> | ||
<string name="app_name">Activities</string> | ||
<string name="button">Open ActivityB</string> | ||
<string name="close_activityc">Close ActivityC</string> | ||
<string name="close_stack">Close Stack</string> | ||
<string name="open_activityd">Open ActivityD</string> | ||
<string name="open_activitya">Open ActivityA</string> | ||
<string name="open_activityc">Open ActivityC</string> | ||
<string name="activity_d">Это активити Д)</string> | ||
</resources> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
#Sat Aug 27 13:57:30 MSK 2022 | ||
distributionBase=GRADLE_USER_HOME | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-bin.zip | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-bin.zip | ||
distributionPath=wrapper/dists | ||
zipStorePath=wrapper/dists | ||
zipStoreBase=GRADLE_USER_HOME |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@NanaOz , я бы использовал тут режим запуска singleTask. Почему:
при этом предусмотрите возможность открывать другие Activity в том же стеке где расположена ActivityA
По клику на кнопку “Open ActivityA” запустите ActivityA, таким образом, чтобы мы попали на существующий экземпляр ActivityA и у него был вызван метод onNewIntent
В таком случае, мы можем и создавать активити выше по стеку в этой задаче и возвращаться в наш экземплят. Синглтоп, что вы используете во флагах вернется в тот же экземпляр только если он на верху стэка. А если он чем-то накрыт, то будет пересоздан. Вот тут, мне кажется, неплохая статейка с примерами