Search code examples
androidandroid-intentthemespreferenceactivityonbackpressed

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


i have an app with 3 activities:main,second,preferences. i can call preferences from main and second one.for preferences i used PreferenceActivity. i my preferences i want to change theme. but the problem is that the theme changed only when activity onCreate.. and when after changes in preferences i press BackBTN, it calls onResume method but not onCreate.

in case of that i made intent to main activity when onBackPressed, but its not the same i want... and i also had a problem when i intent from breferences to mainActivity: after that i click onBackBTN it intent me to Preferences! Its a circle... i tried in Manifest whrite android:noHistory="true" but it doesn't work

here is my code Preferences

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);
}

here is my code 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;
    }
}

here is my code 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();
}

So, 1.how can i get back to previous activity from my Preferences by onBackPressed and use new theme? 2.How to finish app by onBackPressed on main Activity?

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

Solution

  • if you want to exit from app on BackPress then In onBackPress Use

        moveTaskToBack(true);
        finish()
    

    and for another ques i can say take some String with intent by putString and then check condition in another activity by If statement .. if it is true then change theme acc to condition.. i think it will work