Farshad
Farshad

Reputation: 3150

Warning on method chains

Honestly I'm not sure what I am looking for or how to bring it up, but I give it a try:
Sometimes in a class that contains method chains (like builders) we need to warn developer to call a method at the end of chain to take effect, for example in android:

PreferenceManager.getDefaultSharedPreferences(this).edit();   

If you do not call commit() or apply() after putting values in Editor, you get this warning :

SharedPreferences.edit() without a corresponding commit() or apply() call.

I'm trying to implement similar behavior for my class, I mean show warning if a specific method not called at the end of the chain, Any clue or solution will be appreciated

Thanks

Upvotes: 2

Views: 95

Answers (2)

Ivan Wooll
Ivan Wooll

Reputation: 4323

What you are seeing in the case of your SharedPreferences example is a Lint check and is not a behaviour of the class itself. You can implement your own custom checks in Android Studio by following this guide http://tools.android.com/tips/lint-custom-rules

Upvotes: 2

W4R10CK
W4R10CK

Reputation: 5550

What you looking for is a Android Code Design Pattern which is called - Builder Design Pattern.

Builder pattern is a creational design pattern it means its solves problem related to object creation.It is also very oftenly used in android development.

Try and Learn pattern here - Medium

Upvotes: -1

Related Questions