2013-05-03 2 views
0

내 응용 프로그램에 문제가 있습니다 ... 사용자가 응용 프로그램 테마를 선택할 수있는 설정 활동이 있습니다. 그런 다음 "뒤로"라고 표시된 버튼이 있습니다. 사용자를 주 활동으로 초대합니다. 그 후 테마가 저장되고 모든 것이 잘 작동합니다. 하지만 내 문제는 사용자가 "Android back button"을 누르는 경우 내 테마가 저장되지 않는다는 것입니다. 그것을 고칠 수있는 방법이 있습니까?뒤로 버튼을 사용하여 sharedPreferences를 저장합니다.

public class SettingsActivity extends Activity implements OnClickListener { 

public static final String PREF_NAME = "MyPrefsFile"; 
public static int newTheme; 
public final static int THEME_DARK = R.style.DarkTheme; 
public final static int THEME_LIGHT = R.style.LightTheme; 
public final static int THEME_COLORS = R.style.ColorsTheme; 
public final static int THEME_PERF = R.style.Performance; 

@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    SharedPreferences settings = getSharedPreferences(PREF_NAME, MODE_PRIVATE); 
    newTheme = settings.getInt("themeCustom", 0); 

    if(newTheme == THEME_DARK) { 
     setTheme(R.style.DarkTheme); 

    } else if(newTheme == THEME_LIGHT){ 
     setTheme(R.style.LightTheme); 
    } else if(newTheme == THEME_COLORS) { 
     setTheme(R.style.ColorsTheme); 
    } else { 
     setTheme(R.style.Performance); 
    } 
    setContentView(R.layout.activity_settings); 

    findViewById(R.id.button2).setOnClickListener(this); 
    findViewById(R.id.button3).setOnClickListener(this); 
    findViewById(R.id.button4).setOnClickListener(this); 
    findViewById(R.id.button5).setOnClickListener(this); 
    findViewById(R.id.button6).setOnClickListener(this); 
} 
@Override 
public void onClick(View v) { 
    SharedPreferences settings = getSharedPreferences(PREF_NAME, MODE_PRIVATE); 
    SharedPreferences.Editor edit; 
    edit = settings.edit(); 
    Intent intent = getIntent(); 
    Intent main = new Intent(SettingsActivity.this,MainActivity.class); 
    switch (v.getId()) { 
    case R.id.button2: 
     newTheme = THEME_DARK; 
     edit.clear(); 
     edit.putInt("themeCustom", newTheme); 
     edit.commit(); 
     finish(); 
     startActivity(intent); 
     break;   
    case R.id.button3: 
     newTheme = THEME_LIGHT; 
     edit.clear(); 
     edit.putInt("themeCustom", newTheme); 
     edit.commit(); 
     finish(); 
     startActivity(intent); 
     break; 
    case R.id.button5: 
     newTheme = THEME_COLORS; 
     edit.clear(); 
     edit.putInt("themeCustom", newTheme); 
     edit.commit(); 
     finish(); 
     startActivity(intent); 
     break; 
    case R.id.button6: 
     newTheme = THEME_PERF; 
     edit.clear(); 
     edit.putInt("themeCustom", newTheme); 
     edit.commit(); 
     finish(); 
     startActivity(intent); 
     break; 
    case R.id.button4: 
     startActivity(main); 
     break; 
    default: 
     break; 
     }  
    }  
    } 

가 대단히 감사합니다 !! :

여기 내 설정 활동이다 :)

+0

당신이 그것을 가지고 다음과 같은 방법을 사용할 수 있습니까? –

+0

예. 알아 냈습니다. –

답변

1

당신이 onBackPressed() 메소드

@Override 
public void onBackPressed() { 
    super.onBackPressed(); 
    // to do 
} 
+1

슈퍼 기능이 처음 호출 될 때이 방법으로 작동합니까? 저장 기능을 사용하기 전에 활동을 닫지 않습니까? – Gjordis

+0

효과가있었습니다! 대단히 감사합니다 !! :) –

2

당신은 활동이 뒤로 키의 사용자의 보도를 감지하면 onBackpressed()

public void OnBackpressed() 
{ 
// do stuff (same as what you did in your button click) 
} 

은이 호출 할 때 사용할 수 있습니다. 기본 구현은 단순히 현재 액티비티를 끝내기 만하면되지만 원하는대로 수행 할 수 있습니다.

0
@Override 
public void onBackPressed() { 
    //do whatever to save the settings 
    super.onBackPressed(); 
} 

당신이 버튼 + 그것의 원래 의도에 원하는대로 당신이 할 수있는이 방법을 오버라이드 (override) 할 필요가 있습니다.

0

당신은

@Override 
    public void onBackPressed() { 
     // TODO Auto-generated method stub 
     super.onBackPressed(); 
     SharedPreferences settings = getSharedPreferences("PREFS_NAME", 0); 
     SharedPreferences.Editor editor = settings.edit(); 
     editor.putInt("themeCustom", newTheme); 
     editor.commit(); 
    }