android-ktx icon indicating copy to clipboard operation
android-ktx copied to clipboard

Add the ability to get the parent Activity of a View

Open gahfy opened this issue 6 years ago • 8 comments

Adding the ability to get the parent Activity of a View.

gahfy avatar Jul 08 '18 06:07 gahfy

Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

:memo: Please visit https://cla.developers.google.com/ to sign.

Once you've signed (or fixed any issues), please reply here (e.g. I signed it!) and we'll verify it.


What to do if you already signed the CLA

Individual signers
Corporate signers
  • Your company has a Point of Contact who decides which employees are authorized to participate. Ask your POC to be added to the group of authorized contributors. If you don't know who your Point of Contact is, direct the Google project maintainer to go/cla#troubleshoot (Public version).
  • The email used to register you as an authorized contributor must be the email used for the Git commit. Check your existing CLA data and verify that your email is set on your git commits.
  • The email used to register you as an authorized contributor must also be attached to your GitHub account.

googlebot avatar Jul 08 '18 06:07 googlebot

I signed it!

gahfy avatar Jul 08 '18 12:07 gahfy

CLAs look good, thanks!

googlebot avatar Jul 08 '18 12:07 googlebot

Need #582 to be merged otherwise CI won't pass

gahfy avatar Jul 08 '18 12:07 gahfy

Why do you need this? Accessing the activity from a view is a generally discouraged pattern as it creates a bad coupling that prevents reuse.

JakeWharton avatar Jul 09 '18 14:07 JakeWharton

@JakeWharton Most of the time, I use it in BindingAdapters in order to put the Observer process out of the Activity to avoid boilerplate code in Activity which is often the same. For example:

/**
 * Sets a mutable visibility to the View. When the value of the LiveData changes, the visibility of
 * the View will change as well.
 * @param view the View to which to set the mutableVisibility
 * @param visibility the visibility LiveData the View should observe
 */
@BindingAdapter("mutableVisibility")
fun setMutableVisibility(view: View, visibility: MutableLiveData<Int>?) {
    val parentActivity: AppCompatActivity? = view.getParentActivity()
    if(parentActivity != null && visibility != null) {
        visibility.observe(parentActivity, Observer { value -> view.visibility = value?: View.VISIBLE})
    }
}

Then all you have to do is to set app:mutableVisibility as attribute in XML, and you don't have to care about Observers in your Kotlin Activity.

gahfy avatar Jul 10 '18 03:07 gahfy

Rebased on #582 as it has been merged to let CI pass

gahfy avatar Jul 16 '18 15:07 gahfy

Does someone understand the error of instrumented tests and how to solve it?

gahfy avatar Jul 22 '18 06:07 gahfy