首先,我已经知道FragmentManager经常会破坏然后使用默认构造函数重新创建Fragment.编码器必须在工厂方法中将重要事物保存在一组参数中,然后每次在onCreate(Bundle)中重新创建片段时将它们取出.
public class MyFragment extends Fragment {
private static final String MY_STRING_CONSTANT = "param";
private int mIntegerMember;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mIntegerMember= getArguments().getInt(MY_STRING_CONSTANT);
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,这有什么区别:
// Inside MyFragment.java
public MyFragment() {
// No-argument constructor required by the FragmentManager.
}
public static MyFragment newInstance(int param) {
// Factory method
MyFragment fragment = new MyFragment();
Bundle args = new Bundle();
args.putInt(MY_STRING_CONSTANT, param);
fragment.setArguments(args);
return fragment;
}
// Somewhere else
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.add(R.id.frame_layout, MyFragment.newInstance(123)).commit();
Run Code Online (Sandbox Code Playgroud)
还有这个: …
下面是我写过的第一个Java泛型:
public class MyClass {
public static <T> T castToAnotherType(Object param) {
T ret = null;
try {
ret = (T) param;
} catch (ClassCastException e) {
System.out.print("Exception inside castToAnotherType()");
}
return ret;
}
public static void main(String[] args) {
try {
String obj = MyClass.castToAnotherType(new Object());
} catch (ClassCastException e) {
System.out.print("Exception outside castToAnotherType()");
}
}
}
Run Code Online (Sandbox Code Playgroud)
结果是"castToAnotherType()之外的异常".为什么在泛型方法中不会发生异常?
我注意到使用 VSCode 终端时缺少很多命令。ls -l /所以我在我的发行版和 VSCode 的终端上都尝试了。
Linux Mint Xfce 终端:
livy@linux-mint:~$ ls -l /
total 2097252
drwxr-xr-x 2 root root 4096 Aug 13 15:13 bin
drwxr-xr-x 4 root root 4096 Aug 13 15:15 boot
drwxr-xr-x 2 root root 4096 Aug 13 15:06 cdrom
drwxr-xr-x 20 root root 4260 Aug 15 08:40 dev
drwxr-xr-x 147 root root 12288 Aug 13 16:46 etc
drwxr-xr-x 4 root root 4096 Aug 13 15:06 home
lrwxrwxrwx 1 root root 33 Aug 13 …Run Code Online (Sandbox Code Playgroud)