[Bottom UP] 화면 전환 효과(Intent)

2019. 7. 23. 18:01

화면 전환효과가 필요하여 이에 대해 공부해보았다.

기본적인 화면 전환 효과는 버튼에서 다뤘지만, 조금 더 명확하고 간략하게 설명하고자 글을 다시 작성한다.

기본적인 것만 남겨놨더니 다시 볼 때 기억이 잘 안나서 문제였다.

 

우선, 내가 하려는 동작은 버튼을 눌렀을 때, 특정 키 값에 boolean값을 입력하는 (쉐어프리퍼런스) 동작과 키 값을 넣은 후 다음 화면으로 넘어가는 동작이다.

 

화면을 넘어가는 동작의 코드부터 단편적으로 설명하겠다. 전체 코드는 아래와 같다.

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package com.example.applicateclass;
 
import android.content.SharedPreferences;
import com.example.applicateclass.CustomView.CustomSelectBtn;
 
    public class DoctoroActivity extends AppCompatActivity {
        public final String PREFERENCE = "com.example.applicateclass"//저장, 불러오기 위한
        public String auto_key = "auto_key";
        public String direct_key = "direct_key";
        private boolean isBoolean = false;
 
        @Override
    protected void onCreate(Bundle savedInstanceState) {
 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_doctoro);
 
        //data Test
        //final TextView result = (TextView)findViewById(R.id.docotoro_test);
 
        //Select auto and direct
        CustomSelectBtn auto = (CustomSelectBtn) findViewById(R.id.doctoro_auto);
        auto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setPreference(auto_key, !isBoolean); //true
                setPreference(direct_key, isBoolean); //false
              Intent intent = new Intent(
                        getApplicationContext(),
                        SelectGradeActivity.class);
                intent.putExtra("test1", getPerferenceBoolean(direct_key)); //키 확인
                intent.putExtra("test2", getPerferenceBoolean(auto_key)); //키확인
                startActivity(intent);
                overridePendingTransition(00);
            }
        });
 
        CustomSelectBtn direct = (CustomSelectBtn) findViewById(R.id.doctoro_direct);
        direct.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setPreference(direct_key, !isBoolean); //true
                setPreference(auto_key, isBoolean); //false
           //    result.setText("Test Complete! " + getPerferenceBoolean(direct_key) + " " + getPerferenceBoolean(auto_key));
              Intent intent = new Intent(
                        getApplicationContext(),
                        SelectGradeActivity.class);
              intent.putExtra("test1", getPerferenceBoolean(direct_key)); //키확인
              intent.putExtra("test2", getPerferenceBoolean(auto_key)); //키확인
                startActivity(intent);
                overridePendingTransition(00);
            }
        });
 
    }
 
public void setPreference(String key, boolean value){ //데이터 저장 함수
    SharedPreferences pref = getSharedPreferences(PREFERENCE, MODE_PRIVATE);
    editor.putBoolean(key, value);
    }
 
    public boolean getPerferenceBoolean(String key) { //데이터 불러오기(확인용)
        SharedPreferences pref = getSharedPreferences(PREFERENCE, MODE_PRIVATE);
        return pref.getBoolean(key,false);
    }
 
    @Override
    public void onBackPressed() { //화면에서 뒤로가기를 눌렀을 때 변수 초기화
         auto_key = "auto_key";
         direct_key = "direct_key";
        super.onBackPressed();
        overridePendingTransition(00);
    }
 
}
 
 

 

여기서 화면을 전환하는 코드는 아래와 같다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 public class DoctoroActivity extends AppCompatActivity {
 
        @Override
    protected void onCreate(Bundle savedInstanceState) {
 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_doctoro);
 
        //Select auto and direct
        CustomSelectBtn auto = (CustomSelectBtn) findViewById(R.id.doctoro_auto);
        auto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               
              Intent intent = new Intent(
                        getApplicationContext(),
                        SelectGradeActivity.class);
                overridePendingTransition(00); //화면 전환
            }
        });
 
}
 
 
 

Intent를 이용하여 다음 화면으로 이동할 class를 정해주면 다음 화면으로 이동한다.

여기서 부가적으로,   overridePendingTransition(00);는 위로 화면이 올라가며 전환이 되는 기본 화면 전환 효과를 없앤 것이다. 이렇게 하는 편이 앱 이동에 있어서 깔끔했다. 응용하면 다양하게 활용할 수 있을 듯 하다.

BELATED ARTICLES

more