2011-11-03 8 views
5

앱이 시작되거나 다시 시작되면 'SharedPrefences'에 설정된 변수를 기반으로 특정 '활동'으로 사용자를 리디렉션하고 싶습니다.특정 활동으로 Android 앱 실행

private void launchRedirect(Context ctxt) { 

    Integer status = AppPreferences.getStatus(this); 
    Intent i = new Intent(MainActivity.this, Activity1.class); 

    switch (status) { 
    case 0: 
     i = new Intent(MainActivity.this, Activity2.class); 
    case 1: 
     i = new Intent(MainActivity.this, Activity3.class); 
    case 2: 
     i = new Intent(MainActivity.this, Activity4.class); 
    case 3: 
     i = new Intent(MainActivity.this, Activity5.class);  
    } 
    startActivity(i); 
} 

을 그리고 나는 모든 활동에서 각 'onResume'방법이 방법을 호출 할 수 있습니다

내가 된 SharedPreferences 상태 변수를 확인하고 올바른 활동에 리디렉션하는 방법을 가지고 고려하고이 작업을 수행하려면 내 응용 프로그램 :

public void onResume(Bundle savedInstanceState) { 
    launchRedirect(this); 
} 

이 그들이 그것을 호출 할 때, 그것은 onResume 호출하고 현재 사용자에 해당하는 상태로 연결되기 때문에 사용자가 기술적, 마지막 활동에 돌아갈 수 있다는 것을 의미한다.

이것이 순환 버그로 이어질 수 있다고 가정합니다. 더 좋은 방법은 없을까요?

+0

좋은 질문 그래서 +1 –

답변

5

이 상황에서 MainActivity를 종료해야하는 경우 finish() 메소드 호출을 추가 할 수 있다는 것을 제외하면 정상적인 방법이라고 생각합니다. 게다가

는 휴식 문을 잊지 마세요 :

private void launchRedirect(Context ctxt) { 

    Integer status = AppPreferences.getStatus(this); 
    Intent i = new Intent(MainActivity.this, Activity1.class); 

    switch (status) { 
    case 0: 
    i = new Intent(MainActivity.this, Activity2.class); 
    break; 
    case 1: 
    i = new Intent(MainActivity.this, Activity3.class); 
    break; 
    case 2: 
    i = new Intent(MainActivity.this, Activity4.class); 
    break; 
    case 3: 
    i = new Intent(MainActivity.this, Activity5.class); 
    break; 
    } 
    startActivity(i); 
    if (/* check if MainActivity should be closed */) { 
    finish(); 
    } 
} 
+1

아 좋은 지적. 감사. 또한, 나는 아직 투표를하기에 충분한 rep 지점을 가지고 있지 않다. – Asha

+0

그래,이 질문은 나에게 충분한 포인트를 준 :) 당신의 도움을 주셔서 감사합니다 – Asha

+1

당신은 매우 환영합니다 :) 당신도 내 대답을 옳은대로 승인 할 수 있습니까? – morphium

1

당신이 당신의 탐색 activtiy에 따라, 기본 설정 값을 업데이트되어 있는지 확인하십시오. 이렇게하면 활동 시작에 대한 불필요한 점검을 줄일 수 있습니다.

관련 문제