[Bottom UP] 뒤로가기 버튼 동작 설정/Intent로 정보전달

2019. 7. 23. 18:24

뒤로가기 버튼을 눌렀을 때,

 

1. 이전 액티비티로 넘어가는 것을 방지하고자 할 때는 아래와 같이 onBackPressed()에 아무것도 기재하지 않으면 된다.

1
2
3
4
5
 @Override
    public void onBackPressed() { 
        super.onBackPressed();
        //overridePendingTransition(0, 0);
    }
 

 

2. 다음 액티비티로 넘어갔다가, 다시 뒤로가기를 하면 (별도의 설정이 없을 시)

예를 들어 A변수가 true로 설정되며 다음 액티비티로 넘어갔는데 사용자가 잘못 선택한 것이여서 다시 뒤로가기 버튼을 눌러 B선택지를 선택한다면? T F결과와 다르게 T T가 나와버리기 때문에 이 부분에서 설정이 필요하다.

뒤로가기를 할 경우 설정되었던 변수를 초기화 해주는 코드이다.

1
2
3
4
5
6
7
@Override
    public void onBackPressed() { //화면에서 뒤로가기를 눌렀을 때 변수 초기화
         auto_key = "auto_key"//이부분이 초기화
         direct_key = "direct_key"//키값 
        super.onBackPressed();
        overridePendingTransition(00);
    }
 
 

 

3. 이 정보가 잘 초기화되는지를 다음 액티비티에서 확인하고 싶어서 A 액티비티에서 받은 키값의 결과를 B 액티비티에 넘겨주어서 textView에 결과가 의도된대로 나오는지 출력해봤다.

현재 액티비티.java>

1
2
3
4
5
6
7
8
9
    Intent intent = new Intent(
                        getApplicationContext(),
                        SelectGradeActivity.class); //화면 이동 설정
 
                //화면 이동 전 키 Intent로 다음 액티비티에 보내주기
              intent.putExtra("test1", getPerferenceBoolean(direct_key)); 
              intent.putExtra("test2", getPerferenceBoolean(auto_key)); 
                startActivity(intent); //실제 화면 이동
                overridePendingTransition(00);
 

다음 액티비티.java>

1
2
3
4
5
Intent intent = getIntent(); //데이터 수신 확인
        Boolean test1 = intent.getExtras().getBoolean("test1"); //direct
        Boolean test2 = intent.getExtras().getBoolean("test2"); //auto
        TextView testdata = (TextView) findViewById(R.id.grad_what) ; //테스트용 전환
        testdata.setText("direct key:" +test1 + " "+ "auto key: " +test2);
 

정보를 확인하기 위한 getBoolean()은 다음과 같다.

1
2
3
4
 public boolean getPerferenceBoolean(String key) { //데이터 불러오기(확인용)
        SharedPreferences pref = getSharedPreferences(PREFERENCE, MODE_PRIVATE);
        return pref.getBoolean(key,false);
    }
 

 

결과는 잘 나왔다. T T로 나오지 않고 T F 혹은 F T로 작동하는 것을 볼 수 있다.

KakaoTalk_Video_20190723_1822_34_799.mp4
1.47MB

BELATED ARTICLES

more