Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Activity State not getting restored when the activity is killed and restored #24

Closed
danieldisu opened this issue Dec 19, 2017 · 12 comments

Comments

@danieldisu
Copy link

danieldisu commented Dec 19, 2017

Hi

I've find a bug in the library when using ActivityStarter.save(this) in the onSaveInstanceState that causes the state to not be saved and instead restoring the App with the original Intent when setting the debug option Don't keep activities, this setting only forces the system to kill the activity, but it's a behaviour that happens in the wild.

You can see the code reproducing the bug here https://gist.github.com/danieldisu/692c11c1e9eb26a1f27a895c57209a75

Ways to reproduce:

Set "Don't keep activities" in the emulator
Init activity with 2 arguments.
Modify one argument.
Navigate to another app and go back to the app

Expected behaviour
The changed argument is changed and the other one is untouched.

Real behaviour
The arguments are the original ones

@danieldisu
Copy link
Author

The Intent obtained in the getIntent is the original one, it ignores all the changes made during the ActivityStarter.fill().

One way to fix this would be to save the state in the Bundle "outState" that is provided in the onSaveInstanceState. And use the bundle provided in the onCreate method to restore the state if present.

@MarcinMoskala
Copy link
Owner

You are right. I made this change to allow saving state on property delegates (Kotlin). What do you think about the following generation proposition?

Original:

public class MainActivity extends Activity {
  @Arg(optional = true) String name;
}

Generated:

public final class MainActivityStarter {
  private static final String NAME_KEY = "com.example.activitystarter.nameStarterKey";

  public static void fill(MainActivity activity) {
    Intent intent = activity.getIntent();
    if(intent.hasExtra(NAME_KEY))
         activity.name = intent.getStringExtra(NAME_KEY);
  }
  
  public static void save(MainActivity activity, Bundle bundle) {
      bundle.putString("com.example.activitystarter.nameStarterKey", activity.name);
  }

  //...
}

@MarcinMoskala
Copy link
Owner

@danieldisu Can you check version issue_24-SNAPSHOT

compile 'com.github.MarcinMoskala:ActivityStarter:issue_24-SNAPSHOT'

@danieldisu
Copy link
Author

danieldisu commented Dec 20, 2017

Thank you for you fast response!!

I've tried it and is still not working, it's almost there!

The Save method now is 100% OK, but it's also needed to change the FILL method, to also receive the bundle, in order to load the saved info from it when present.
The thing is that you have to check if the bundle is null, if it's null load it from the intent, if not, load it from the bundle!

@MarcinMoskala
Copy link
Owner

I think it should be ok in current SNAPSHOT

@danieldisu
Copy link
Author

The generic method ActivityStarter.fill() still doesn't have the possibility to pass the bundle.

But using the ActivityNameStarter.fill(activity, bundle) method works like a charm!

Do you think it would be possible to add an overloaded method ActivityStarter.fill() that receives the Bundle?

@MarcinMoskala
Copy link
Owner

In the published version, there is ActivityStarter.fill(this, savedInstanceState); Sorry for the delay, but in the moment I am really overloaded with projects. Although I tested it and here you can see example usage.

@danieldisu
Copy link
Author

Everything is working now!

Thanks!!

@MarcinMoskala
Copy link
Owner

But this is not the solution because it does not allow Kotlin property delegates to read state from saved bundle. I am still working on this issue.

@danieldisu
Copy link
Author

I see the problem, but it's going to be a hard one to crack because there is no way to access the savedInstanceState Bundle outside the lifecycle methods.

@MarcinMoskala
Copy link
Owner

I see now that there is no way to do it. I will fill values if savedInstanceState is not null and if the property is read-write.

@MarcinMoskala
Copy link
Owner

Today I finally had more time to fix this problem. From new version, Kotlin needs also fill called for loading saved state (if we need it). I just distributed it in beta version 1.10

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants