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

fix: use default constructor / factory + Arguments init style #17488

Merged
merged 1 commit into from
Nov 26, 2024

Conversation

mikehardy
Copy link
Member

@mikehardy mikehardy commented Nov 23, 2024

Purpose / Description

this allows Fragment lifecycle save/restore to work seemlessly and that fixes crashes on Activity restarts

NOTE THAT IF THIS STYLE IS OKAY - A FEW OTHER DIALOGS NEED FIXES IN SAME STYLE

[edit: turns out this was the only easy one like this - so just one commit]

Fixes

Approach

Used the approach mentioned here https://stackoverflow.com/a/51831137/9910298

  • Stop providing params to constructor, so there is a default constructor as required by default Fragment factory
  • Start providing params as a Bundle set in to Fragment.arguments via a static newInstance method
  • Access all params via Fragment.arguments
  • Handle optionally missing arguments with warnings+defaults

How Has This Been Tested?

  • Enable developer option Don't Keep Activities
  • Go to Card Browser, open Actions Menu "Options"
  • Background AnkiDroid, resume AnkiDroid --> crash without patch
  • With patch check all toggles + background/foreground permutations

Everything seems to work

browser-options-crashfix.webm

Learning (optional, can help others)

Now we don't just have Activity lifecycle problems, we have Fragment lifecycle problems. Great

Checklist

Please, go through these checks before submitting the PR.

  • You have a descriptive commit message with a short title (first line, max 50 chars).
  • You have commented your code, particularly in hard-to-understand areas
  • You have performed a self-review of your own code
  • UI changes: include screenshots of all affected screens (in particular showing any new or changed strings)
  • UI Changes: You have tested your change using the Google Accessibility Scanner

@mikehardy

This comment was marked as outdated.

Copy link
Member

@BrayanDSO BrayanDSO left a comment

Choose a reason for hiding this comment

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

Fully agreed with empty constructors for Fragments and passing stuff with arguments. Following the Android way of things is normally the most sane alternative.

@BrayanDSO BrayanDSO added the Needs Second Approval Has one approval, one more approval to merge label Nov 23, 2024
@mikehardy
Copy link
Member Author

Okay, going to take that preliminary note of approval for the style, Kotlin-ify it, and try another Dialog Fragment or two

@mikehardy mikehardy marked this pull request as draft November 23, 2024 21:58
david-allison

This comment was marked as resolved.

Copy link
Member

@lukstbit lukstbit left a comment

Choose a reason for hiding this comment

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

Yes, dialogs with constructor parameters that don't use a fragment factory should be refactored.

@mikehardy
Copy link
Member Author

Turns out there was only one other DialogFragment with a problem like this (no default constructor) and it has a Factory but it must just not get installed correctly every time. I'm not going to add that commit here, I'll work it in a separate PR

So this is ready to go

I've adopted all the feedback to make this more Kotlin-y (and extract string constants) - thank you!

Three people approved but there were enough little changes from the feedback I think at least one other person should take a final look before merge so I am repinging the reviewers but hopefully (since it is a simple change) it's easy and done

Copy link
Member

@david-allison david-allison left a comment

Choose a reason for hiding this comment

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

Minor improvement with constants I believe, code is good

Although I stated that the styles of getter was inconsistent, I prefer them the way they are now.

The following looks ugly to me

    private val isTruncated by lazy {
        when (arguments?.getBoolean("isTruncated")) {
            true -> true
            false -> false
            null -> {
                Timber.w("BrowserOptionsDialog instantiated without configuration.")
                false
            }
        }
   

Comment on lines +43 to +46
@IdRes val selectedButtonId =
dialogView.findViewById<RadioGroup>(R.id.select_browser_mode).checkedRadioButtonId
val newCardsOrNotes =
if (selectedButtonId == R.id.select_cards_mode) CardsOrNotes.CARDS else CardsOrNotes.NOTES
Copy link
Member

Choose a reason for hiding this comment

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

Note: this is now just a spacing change

@david-allison david-allison added the Needs Author Reply Waiting for a reply from the original author label Nov 26, 2024
this allows Fragment lifecycle save/restore to work seemlessly and
that fixes crashes on Activity restarts
@mikehardy mikehardy removed the Needs Author Reply Waiting for a reply from the original author label Nov 26, 2024
@david-allison david-allison added Pending Merge Things with approval that are waiting future merge (e.g. targets a future release, CI wait, etc) and removed Needs Second Approval Has one approval, one more approval to merge labels Nov 26, 2024
@david-allison david-allison added this pull request to the merge queue Nov 26, 2024
Merged via the queue into ankidroid:main with commit c84fb10 Nov 26, 2024
9 checks passed
@github-actions github-actions bot removed the Pending Merge Things with approval that are waiting future merge (e.g. targets a future release, CI wait, etc) label Nov 26, 2024
@github-actions github-actions bot modified the milestones: 2.19.3 release, 2.20 Release Nov 26, 2024
@mikehardy mikehardy deleted the dialog-fragment-crash-fix branch November 26, 2024 12:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants