我有一个包含 3 个活动的应用程序:主要、次要、首选项。我可以从主要和第二个调用首选项。对于我使用 PreferenceActivity 的首选项。我的喜好我想改变主题。但问题是主题仅在活动 onCreate 时发生变化。当偏好更改后我按下 BackBTN 时,它调用 onResume 方法而不是 onCreate。
在这种情况下,我在 onBackPressed 时对主要活动做出了意向,但它与我想要的不一样......当我打算从 breferences 到 mainActivity 时我也遇到了问题:之后我点击 onBackBTN 它打算让我进入偏好!它是一个圆圈...我在 Manifest 中尝试了写入android:noHistory="true"
,但它不起作用
这是我的代码首选项
public class Preferences extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
@Override
public void onResume(){
super.onResume();
getPreferenceManager().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
}
@Override
public void onPause(){
super.onPause();
getPreferenceManager().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
ListPreference listPref = (ListPreference)findPreference(getString(R.string.ListPref_key_1));
CharSequence currText = listPref.getEntry();
if (currText.toString().contains(getString(R.string.chooseThemeDayText))){
Utils.changeToTheme(this,Utils.THEME_DAY);
} else if (currText.toString().contains(getString(R.string.chooseThemeNightText))){
Utils.changeToTheme(this,Utils.THEME_NIGHT);
}
}
@Override
public void onBackPressed(){
Intent intent = new Intent(this,MainActivity.class);
startActivity(intent);
}
这是我的代码
public class Utils {
private static int sTheme;
public final static int THEME_DAY = 0;
public final static int THEME_NIGHT = 1;
//Set the theme of the Activity, and restart it by creating a new Activity of the same type.
public static void changeToTheme(Activity activity, int theme) {
sTheme = theme;
activity.finish();
activity.startActivity(new Intent(activity, activity.getClass()));
}
// Set the theme of the activity, according to the configuration.
public static void onActivityCreateSetTheme(Activity activity){
switch (sTheme){
default:
case THEME_DAY:
activity.setTheme(R.style.ThemeDay);
break;
case THEME_NIGHT:
activity.setTheme(R.style.ThemeNight);
break;
}
}
这是我的代码mainActivity
public class MainActivity extends Activity {
public PowerManager.WakeLock myWakeLock;
InputStream is;
AssetManager am;
Intent intent ;
public String taleName_str1,taleName_str2;
Button btn_tale1,btn_tale2;
int size;
byte[] buffer;
public static String TAG = "myLogs";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Utils.onActivityCreateSetTheme(this);
setContentView(R.layout.activity_main);
onWakeLock();
btn_tale1 = (Button) findViewById(R.id.btn_tale1);
btn_tale2 = (Button) findViewById(R.id.btn_tale2);
}
public void onClickRead(View view) {
switch (view.getId()){
case R.id.btn_tale1:
intent = new Intent("ua.andriyantonov.tales.tale1");
startActivity(intent);
break;
case R.id.btn_tale2:
intent = new Intent("ua.andriyantonov.tales.tale2");
startActivity(intent);
break;
}
}private void getTaleNames() {
am = getAssets();
try {
//taleName1
is = am.open("tale1_name.txt");
size = is.available();
buffer = new byte[size];
is.read(buffer);
is.close();
taleName_str1 = new String(buffer);
btn_tale1.setText(taleName_str1);
//taleName2
is=am.open("tale2_name.txt");
size=is.available();
buffer = new byte[size];
is.read(buffer);
is.close();
taleName_str2=new String(buffer);
btn_tale2.setText(taleName_str2);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.mainSettings:
intent = new Intent(this, Preferences.class);
startActivity(intent);
break;
}
return super.onOptionsItemSelected(item);
}
private void onWakeLock(){
final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
this.myWakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My tag");
this.myWakeLock.acquire();
}
那么, 1.我如何通过 onBackPressed 从我的首选项中返回之前的活动并使用新主题? 2.如何在主Activity上通过onBackPressed完成app?
02-05 14:48:54.845 19539-19539/ua.andriyantonov.tales D/libEGL﹕ loaded /system/lib/egl/libEGL_mali.so
02-05 14:48:54.855 19539-19539/ua.andriyantonov.tales D/libEGL﹕ loaded /system/lib/egl/libGLESv1_CM_mali.so
02-05 14:48:54.865 19539-19539/ua.andriyantonov.tales D/libEGL﹕ loaded /system/lib/egl/libGLESv2_mali.so
02-05 14:48:54.875 19539-19539/ua.andriyantonov.tales D/OpenGLRenderer﹕ Enabling debug mode 0
02-05 14:48:56.146 19539-19539/ua.andriyantonov.tales D/myLogs﹕ start
02-05 14:48:56.206 19539-19539/ua.andriyantonov.tales W/MediaPlayer﹕ mediaplayer went away with unhandled events
02-05 14:48:56.206 19539-19539/ua.andriyantonov.tales W/MediaPlayer﹕ mediaplayer went away with unhandled events
02-05 14:48:57.507 19539-19539/ua.andriyantonov.tales D/AndroidRuntime﹕ Shutting down VM
02-05 14:48:57.507 19539-19539/ua.andriyantonov.tales W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x40dd1318)
02-05 14:48:57.517 19539-19539/ua.andriyantonov.tales E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.IllegalStateException: Could not find a method onClickRead(View) in the activity class ua.andriyantonov.tales.TaleActivity for onClick handler on view class android.widget.Button with id 'btn_tale2'
at android.view.View$1.onClick(View.java:3590)
at android.view.View.performClick(View.java:4103)
at android.view.View$PerformClick.run(View.java:17117)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4744)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NoSuchMethodException: onClickRead [class android.view.View]
at java.lang.Class.getConstructorOrMethod(Class.java:460)
at java.lang.Class.getMethod(Class.java:915)
at android.view.View$1.onClick(View.java:3583)
at android.view.View.performClick(View.java:4103)
at android.view.View$PerformClick.run(View.java:17117)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4744)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
02-05 14:48:59.920 19539-19539/ua.andriyantonov.tales I/Process﹕ Sending signal. PID: 19539 SIG: 9