原文:
How to automatically generate getters and setters in Android Studio
方法也非常簡單,就是在你要用的程式碼上按 Alt+ Insert
剩下的自己研究研究吧!
相關資料:
new MyFragment()
和
MyFragment.newInstance()
有什麼不同?哪個比較好?
答: 使用
newInstance()比較好,兩個原因:What is the difference betweennew MyFragment()
andMyFragment.newInstance()
? Should I prefer one over the other?
newInstance()
method is a "static factory method," allowing us to initialize and setup a new Fragment
without having to call its constructor and additional setter methods. Providing static factory methods for your fragments is good practice because it encapsulates and abstracts the steps required to setup the object from the client. For example, consider the following code:public class MyFragment extends Fragment {
/**
* Static factory method that takes an int parameter,
* initializes the fragment's arguments, and returns the
* new fragment to the client.
*/
public static MyFragment newInstance(int index) {
MyFragment f = new MyFragment();
Bundle args = new Bundle();
args.putInt("index", index);
f.setArguments(args);
return f;
}
}