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

Field ID generation #14

Merged
merged 1 commit into from Nov 28, 2015
Merged

Conversation

aveuiller
Copy link
Contributor

The fix wasn't as easy as expected, but I have something. :)

I had to create every field View programmatically, because xml layouts will end with the same bug as #12, i.e. every field of the same type will share the same id, so Android will recognize them as the same and copy values between them on rotation.
So the Idea is that every field has a new ID when created.

At this point, the rotation creates new fields (thus with different IDs), so their values weren't kept.
I had to make the FormModel Serializable in order to save it upon Activity destruction and re-set it on activity creation (which are called on a phone rotation without special treatment).

This will Close #11

@dkharrat
Copy link
Owner

Great! I appreciate the effort. I'll review the code soon and let you know of any feedback.


/**
* <code>FormModel</code> is an abstract class that represents the backing data for a form. It provides a mechanism
* for form elements to retrieve their values to display to the user and persist changes to the model upon changes.
*/
public abstract class FormModel {
public abstract class FormModel implements Serializable {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if there's another way we can maintain the model instance on rotations besides implementing the Serializable interface. It's reasonable, but my main concern is that it might be a bit too restricting for users if they need to implement their own model. This essentially means their model and its class members must be Serializable.

It might be a reasonable restriction of there's no other way, but I'd like to make sure we explore other options too. Were there other ideas you considered?

@aveuiller
Copy link
Contributor Author

I mainly looked at the android documentation, which provides two ways of doing it:

  1. Handle the configuration on the activity itself,
    it prevents Android from recreating the view on rotation.
    The main drawback being that it has to be declared on the manifest (so it is the user's responsability to declare it for each final activity).
  2. Use a fragment and configure it to be retained between activity creations.
    I originally tried to use this method on the FormController, which was clearly a bad idea, but looking back at the documentation made me realize It is possible to use this with the FormModel instead.

I tried the second option and it works well, making the FormModel a Fragment instead of Serializable, and using a FragmentActivity as a basis for the FormActivity.
Would that solution suits you better?

@dkharrat
Copy link
Owner

From those 2 options, I'd say approach 2 is definitely better.

Would you please squash your commits to 1 so I can merge it?

@@ -11,7 +11,7 @@
* {@link FormWithAppCompatActivity}
*/
public abstract class FormActivity extends Activity {

private static final String MODEL_KW = "model";
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what the KW part stands for. What do you think of MODEL_BUNDLE_KEY as the constant name? Also MODEL_KEY works.

Also, let's prefix the value with nd_ (e.g. nd_model) to prevent collisions in case the the user also persists data in the Bundle.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

KW stood for keyword, I will change it to MODEL_BUNDLE_KEY.

Having static IDs made the application crash on rotation, because of the
reconstruction of the FormActivity.

This commit protect the form against this crash, generating unique ID
for each view and saving the FormData (as a retained fragment) between
Activity reconstruction.
dkharrat added a commit that referenced this pull request Nov 28, 2015
fix crash caused by field view ID collision
@dkharrat dkharrat merged commit d159b96 into dkharrat:master Nov 28, 2015
@dkharrat
Copy link
Owner

Merged the changes to master. Thanks for your contribution @aveuiller!

flibbertigibbet added a commit to flibbertigibbet/NexusDialog that referenced this pull request Mar 14, 2016
@aveuiller aveuiller deleted the fix-fieldIdGeneration branch March 18, 2016 12:13
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

Successfully merging this pull request may close these issues.

Crash on screen rotation with SelectionController.
2 participants