getodk / collect

ODK Collect is an Android app for filling out forms. It's been used to collect billions of data points in challenging environments around the world. Contribute and make the world a better place! ✨📋✨
https://docs.getodk.org/collect-intro
Other
719 stars 1.37k forks source link

Dialogs are disappearing after screen rotation. #3612

Closed kkrawczyk123 closed 3 years ago

kkrawczyk123 commented 4 years ago

Software and hardware versions

Collect v1.25.1, Android v4 -10.x.x, device used...

Problem description

Displayed dialogs are disappearing after screen rotation.

Dialogs: Exit form dialog Remove this response Layer data file dialog on geowidgets with map Input method dialog on geotrace and geoshape dialog Change Admin Password Dialog

Steps to reproduce the problem

  1. Open one of the dialogs described above
  2. Rotate the screen

Expected behavior

Dialogs should not disappear after rotating the screen.

Other information

I have noticed behavior differences between Android 8.1, 9.0 and other devices on Collect 1.23.3 - dialogs are not disappearing on them after rotating the screen. From 1.24.0 it occurs on all devices.

getodk-bot commented 4 years ago

Hello @grzesiek2010, you have been unassigned from this issue because you have not updated this issue or any referenced pull requests for over 15 days.

You can reclaim this issue or claim any other issue by commenting @opendatakit-bot claim on that issue.

Thanks for your contributions, and hope to see you again soon!

seadowg commented 4 years ago

I reckon we could solve this by moving these dialogs to using DialogFragment rather than AlertDialog. This would mean the dialogs should be recreated when the Activity goes through the configuration changes (as far as I'm aware).

SaumiaSinghal commented 4 years ago

Hie @seadowg! Can I work on this?

seadowg commented 4 years ago

@SaumiaSinghal sure! Go ahead and claim it! I'd look at the DialogFragments we're already using like ProgressDialogFragment and MaterialFullScreenDialogFragment and their tests to get a sense of how to work with the DialogFragment. We've also got some helpers that will be useful for opening/closing DialogFragment instances in DialogUtils.

SaumiaSinghal commented 4 years ago

@opendatakit-bot claim

getodk-bot commented 4 years ago

Hello @SaumiaSinghal, you have been unassigned from this issue because you have not updated this issue or any referenced pull requests for over 15 days.

You can reclaim this issue or claim any other issue by commenting @opendatakit-bot claim on that issue.

Thanks for your contributions, and hope to see you again soon!

seadowg commented 3 years ago

We talked a while ago about being more lenient with these, as it's a lot of investment for little gain to keep dialogs on screen during configuration changes. Closing this, but we can create separate issues if anyone feels one of these cases needs to be fixed.

VaibhavMojidra commented 9 months ago

Ok After working on this for 1 Week I found the solution:

Of course the correct solution to above issue is DialogFragement Still there was some issues I was facing as I wanted to make it resusable and sometimes it was not dismissing after any system change like rotating, changing of dark to light mode (Was able to continue appearing in screen but dismissing was disabled) etc..

This problem will resolve issues :

  1. Disappearing of Dialog when screen rotates/System changes.
  2. Data will persistent on dialog (Dialog Title and other stuff).
  3. Will be able to dismiss after system changes/rotate.

Note: I have done this in Kotlin. But concept is same please read comments its very important


ConfirmDialogWithYesAndNo.kt

class ConfirmDialogWithYesAndNo(private val confirmDialogTitleText:String="Default Confirm Dialog Title",private val confirmDialogMessageText:String="Default Confirm Dialog Message",private val confirmDialogPositiveButtonText:String="Yes",private val confirmDialogNegativeButtonText:String="No",private val confirmDialogPositiveButtonClickListener:()->Unit= {},private val confirmDialogNegativeButtonClickListener:()->Unit= {}): DialogFragment(){
    //Must keep this global
    private lateinit var alertDialog: AlertDialog

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //Must use this otherwise after system change it will not dismiss and won't have same text which you passed.
        retainInstance = true
    }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        super.onCreateDialog(savedInstanceState)

        //Normal Dialog creation and return it
        val builder = AlertDialog.Builder(requireActivity())
        builder.setTitle(confirmDialogTitleText)
        builder.setMessage(confirmDialogMessageText)
        builder.setIcon(android.R.drawable.ic_dialog_alert)
        builder.setPositiveButton("Yes") { _, _ ->
            //In case you use custom layout
            alertDialog.dismiss()

            confirmDialogPositiveButtonClickListener()
        }
        builder.setNegativeButton("No") { _, _ ->
            //In case you use custom layout button
            confirmDialogNegativeButtonClickListener()
        }
        val alertDialog: AlertDialog = builder.create()
        alertDialog.setCancelable(false)
        alertDialog.show()

        return alertDialog
    }
}

Usage

MainActivity.kt:

class MainActivity : AppCompatActivity() {
    ....
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
.....
        butoon.setOnClickListener {
            val confirmDialogWithYesAndNo= ConfirmDialogWithYesAndNo("Hiii T","Hi Message","Yess B","No B",{
                Toast.makeText(this,"Yesss",Toast.LENGTH_LONG).show()
            },{
                Toast.makeText(this,"Noo",Toast.LENGTH_LONG).show()
            })
            confirmDialogWithYesAndNo.showNow(this.supportFragmentManager,"anyrandomstring")
          ...
        }
       ....

    }
}

To contact or to check this code example : vaibhavmojidra.github.io/site/