티스토리 뷰
Android/Java
[Java] Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
혀가 길지 않은 개발자 2020. 9. 2. 16:47Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1 페이지"
android:textSize="30dp" />
<Button
android:id="@+id/btnMain"
android:layout_width="220dp"
android:layout_height="80dp"
android:text="다음 페이지로 이동"
android:textSize="22dp"
android:layout_marginTop="100dp"/>
</LinearLayout>
activity_sub.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".SubActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2 페이지"
android:textSize="30dp" />
<Button
android:id="@+id/btnSub"
android:layout_width="220dp"
android:layout_height="80dp"
android:text="처음으로 이동"
android:textSize="22dp"
android:layout_marginTop="100dp"/>
</LinearLayout>
SubActivity.java
package com.jwsoft.javaproject;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class SubActivity extends AppCompatActivity {
private Button btnSub;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sub);
btnSub = findViewById(R.id.btnSub);
btnSub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
}
});
}
}
MainActivity.java
package com.jwsoft.javaproject;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button btnMain;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnMain = findViewById(R.id.btnMain);
btnMain.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), SubActivity.class);
startActivity(intent);
}
});
}
}
activity가 스택처럼 쌓인다.
SubActivity.java
package com.jwsoft.javaproject;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class SubActivity extends AppCompatActivity {
private Button btnSub;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sub);
btnSub = findViewById(R.id.btnSub);
btnSub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}
});
}
}
SubActivity.java 에만
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 추가
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); 추가
MainActivity.java
package com.jwsoft.javaproject;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button btnMain;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnMain = findViewById(R.id.btnMain);
btnMain.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), SubActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}
});
}
}
MainActivity.java 에도
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 추가
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); 추가
'Android > Java' 카테고리의 다른 글
[Java] Tic-tac-toe 게임으로 보는 MVP 패턴 (0) | 2020.09.18 |
---|---|
[Java] Tic-tac-toe 게임으로 보는 MVC 패턴 (0) | 2020.09.17 |
[Java] InputMethodManager (0) | 2020.08.25 |
[Java] STT (Speech-to-Text) (0) | 2020.08.24 |
[Java] ArrayList.remove() (0) | 2020.08.20 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- fragment
- Livedata
- coroutine
- James Kim
- 자바
- 코틀린
- java
- DataBinding
- handler
- JSONArray
- Kotlin
- recyclerview
- 혀가 길지 않은 개발자
- Design Pattern
- TabLayout
- MVVM
- Architecture Pattern
- ArrayList
- ViewModel
- XML
- JSONObject
- Intent
- View
- activity
- CoordinatorLayout
- Android
- 안드로이드 #코틀린 #Android #Kotlin
- 안드로이드
- ViewPager2
- Vue.js #Vue.js + javascript
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함