如何通过 onBackPressed() 回到之前的 activity 并使用新主题?

How get back to previous activity by onBackPressed() and use new theme?

我有一个应用程序有 3 个 activities:main,第二个,首选项。 我可以从主要和第二个 one.for 偏好中调用偏好,我使用了 PreferenceActivity。 我的喜好我想改变主题。但问题是主题仅在 activity onCreate.. 在更改首选项后我按 BackBTN,它会调用 onResume 方法但不会调用 onCreate。

在这种情况下,我打算在 onBackPressed 时主 activity,但它与我想要的不一样...... 当我打算从 breferences 到 mainActivity 时,我也遇到了一个问题:之后我单击 onBackBTN,它让我转到 Preferences!是一个圆... 我在 Manifest whrite 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);
}

这是我的代码 Utils

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.how 我可以通过 onBackPressed 从我的偏好设置返回到之前的 activity 并使用新主题吗? 2.How 在主 Activity?

上通过 onBackPressed 完成应用程序
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.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.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
   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);

只需将这些行复制到您的 MainActivity 的 onResume() 函数中,然后从您的首选项中删除 onBackPressed() 函数 class。

添加行

super.onResume();

onResume() 函数 在您粘贴的行之前

如果您想在 BackPress 上退出应用程序,那么在 onBackPress 使用

    moveTaskToBack(true);
    finish()

对于另一个问题,我可以说 putString 使用 intent 获取一些字符串 然后通过 If 语句在另一个 activity 中检查条件 .. 如果它是真的然后将主题 acc 更改为条件 .. 我认为它会工作