0

To clear things up, I want my SettingsActivity.class to restart. Everything works except finish(), which doesn't seem to work at all. My code creates a new SettingsActivity.class but doesn't destroy the old one.

private SharedPreferences.OnSharedPreferenceChangeListener listener1 = new SharedPreferences.OnSharedPreferenceChangeListener() {
    public void onSharedPreferenceChanged(SharedPreferences preferences, String key) {
        if (key.equals("tema1")) {
            finish();
            startActivity(getIntent());
        }
        if (key.equals("tema2")) {
            finish();
            startActivity(getIntent());
        }
        if (key.equals("notification")) {
            recreate();
        }

The problem is therefore that finish() does not work. It simply doesn't destroy the activity. As I said, everything else works.

Edit Finish() still doesn't work and I'd like an answer to that, but I managed to make a workaround by using the intentflag "clear top" for the startActivity intent. Achieves the same effect as I wanted, without even using finish().

  • you can recreate the intent , instead of using the same old one , so try `startActivity(YourActivityName.this,YourActivityName.class)` and call `finish()` after – Pavneet_Singh Jan 29 '17 at 12:24
  • @PavneetSingh That gives error "startActivity in Activity cannot be applied to SettingsActivity". – Martin R. L. Jan 29 '17 at 12:30
  • add the complete error in your post at the end – Pavneet_Singh Jan 29 '17 at 12:41
  • @PavneetSingh That is the complete error. (Activity.this, Activity.class) gets underlined red and when hovering the mouse it says what I wrote. – Martin R. L. Jan 29 '17 at 12:55
  • it should be `startActivity(SettingsActivity.this,SettingsActivity.class)` – Pavneet_Singh Jan 29 '17 at 14:07
  • @PavneetSingh That's what I did. The problem really is that the onCreate or onSharedPreferencesChanged acts like it's static. – Martin R. L. Jan 29 '17 at 14:12
  • oh ho , move your `listener = new SharedPreferences.OnSharedPreferenceChangeListener() {` code outside `oncreate` – Pavneet_Singh Jan 29 '17 at 14:18
  • 1
    @PavneetSingh Then "onSharesPreferenceChanged" is never used? Can you give a normal answer including code what you mean please? – Martin R. L. Jan 29 '17 at 14:35
  • What is the purpose of two `if` statements which have the same code inside them? More importantly, why do you need to restart the activity? – Code-Apprentice Jan 29 '17 at 16:20
  • Because it's two switches in the preferencefragment, and those switches switches a boolean value, that tells all other activities to change theme. To have this happen to SettingsActivity right when the switch is pressed, I need to restart it, for the theme to take immediate effect, preferably with a nice transition. Two if statements could probably be fixed, but right now i just want the finish() to work properly, as it's not destroying the activity as it should. – Martin R. L. Jan 29 '17 at 16:46
  • @Code-Apprentice Which means the thing we should focus on is getting finish() to do it's job. – Martin R. L. Jan 29 '17 at 17:09
  • @MartinR.L. I think you need to find the correct way to change your app's theme while it is running. I suggest reading http://stackoverflow.com/questions/18301554/android-change-app-theme-on-onclick#18301723 and the question linked at the top. – Code-Apprentice Jan 29 '17 at 19:57
  • @Code-Apprentice No the theme switches work with no problem. It's just that I'm trying to make the current activity switch too, with a nice animation. I can use recreate() instead of finish() and startActivity() but that is very ugly and stutters the app for a second. The only problem is that the finish() doesnt work and the result is a new SettingsActivity starts but the old one isn't destroyed. Which means pressing the back button after a theme switch takes the user back to SettingsActivity with the first theme, and so on. I simply need the finish() to work. – Martin R. L. Jan 29 '17 at 20:02
  • @MartinR.L. If you take a minute to read the link I gave you, it will show you the solution. – Code-Apprentice Jan 29 '17 at 20:06
  • Possible duplicate of [Android - Change app Theme on onClick](http://stackoverflow.com/questions/18301554/android-change-app-theme-on-onclick) – Code-Apprentice Jan 29 '17 at 20:07
  • @Code-Apprentice I did. That solution is practically the same except splitting the code up in a utils file. Still activity.finish() and activity.startActivity(). I could do that, but it is not preffered when it should be possible to do it right on spot like I'm trying to. I guess I'll do it anyways since there doesn't seem to be a solution to this. Another alternative would be to use recreate() and somehow get a transition animation to work with it, but from what I've read that isn't possible. – Martin R. L. Jan 29 '17 at 20:11
  • @MartinR.L. There is one very critical difference. – Code-Apprentice Jan 29 '17 at 20:22
  • @Code-Apprentice Alright, I will try doing like that link. If you have, explain or send a link explaining the difference. I'm a big noob in android development (started a few weeks ago) so I'll take all knowledge I can :) – Martin R. L. Jan 29 '17 at 20:29
  • @MartinR.L. try calling finish() first – Code-Apprentice Jan 29 '17 at 20:40
  • @Code-Apprentice If you look at the original code, that's what I did. More evidence on the fact that finish() doesn't work, is that the whole activity should close and no new should open with finish() after startActivity(). – Martin R. L. Jan 30 '17 at 05:39

0 Answers0