调用 Fragment 的空构造函数而不是 newInstance
Empty constructor of Fragment called instead of newInstance
我在开发的 android 应用程序中遇到一些奇怪的行为:
我有一个 class 提示菜单(片段):
public class HintsMenu extends Fragment implements AbsListView.OnItemClickListener {
...
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static HintsMenu newInstance() {
System.out.println("HintsMenu.newInstance()");
HintsMenu fragment = new HintsMenu();
Bundle args=new Bundle();
args.putString(ARG_TITLE,"Hints");
fragment.setArguments(args);
return fragment;
}
/**
* Mandatory empty constructor for the fragment manager to instantiate the
* fragment (e.g. upon screen orientation changes).
*/
public HintsMenu() {
System.out.println("Hintsmenu constructor");
}
此片段在 activity:
中动态使用
public class MainActivity extends ActionBarActivity implements MainMenu.NavigationDrawerCallbacks {
....
@Override
public void onNavigationDrawerItemSelected(MenuEntry item) {
// update the main content by replacing fragments
if (item.hasActivity()){
...
} else if (item.hasFragment()){
try {
FragmentManager fragmentManager = getSupportFragmentManager();
Class<? extends Fragment> cl=item.getFragmentClass();
System.out.println("Calling " + cl.getSimpleName() + ".newInstance()...");
Fragment newFragment=cl.newInstance();
fragmentManager.beginTransaction().replace(R.id.container, newFragment).commit();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
这个想法是,item.activityClass(代码未显示)由表达式
设置
item.fragmentClass=HintsMenu.class;
在代码的另一个位置,item.getFragmentClass() returns 这个值在 onNavigationDrawerItemSelected() 中。
在 "onNavigationDrawerItemSelected" 中,使用 cl.newInstance() 调用创建了一个新的 HintsMenu。
但是,没有调用 HintsMenu 的 newInstance 方法,而是调用了空的 Constructor。
所以安装了预期的输出
Calling HintsMenu.newInstance()...
HintsMenu.newInstance()
Hintsmenu constructor
我只得到
Calling HintsMenu.newInstance()...
Hintsmenu constructor
如果我这样做
HintsMenu.newInstance()
我得到了预期的输出。
谁能告诉我,我做错了什么?
HintsMenu
上的静态 newInstance()
不是 the newInstance()
method on Class
。您正在调用后者。例如,如果您要将 HintsMenu
上的 newInstance()
重命名为 newMenu()
,并且您将 onNavigationDrawerItemSelected()
实现更改为尝试调用 newMenu()
,您将得到编译错误,因为编译器会抱怨它无法在 Class
.
上找到 newMenu()
方法
我在开发的 android 应用程序中遇到一些奇怪的行为:
我有一个 class 提示菜单(片段):
public class HintsMenu extends Fragment implements AbsListView.OnItemClickListener {
...
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static HintsMenu newInstance() {
System.out.println("HintsMenu.newInstance()");
HintsMenu fragment = new HintsMenu();
Bundle args=new Bundle();
args.putString(ARG_TITLE,"Hints");
fragment.setArguments(args);
return fragment;
}
/**
* Mandatory empty constructor for the fragment manager to instantiate the
* fragment (e.g. upon screen orientation changes).
*/
public HintsMenu() {
System.out.println("Hintsmenu constructor");
}
此片段在 activity:
中动态使用public class MainActivity extends ActionBarActivity implements MainMenu.NavigationDrawerCallbacks {
....
@Override
public void onNavigationDrawerItemSelected(MenuEntry item) {
// update the main content by replacing fragments
if (item.hasActivity()){
...
} else if (item.hasFragment()){
try {
FragmentManager fragmentManager = getSupportFragmentManager();
Class<? extends Fragment> cl=item.getFragmentClass();
System.out.println("Calling " + cl.getSimpleName() + ".newInstance()...");
Fragment newFragment=cl.newInstance();
fragmentManager.beginTransaction().replace(R.id.container, newFragment).commit();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
这个想法是,item.activityClass(代码未显示)由表达式
设置item.fragmentClass=HintsMenu.class;
在代码的另一个位置,item.getFragmentClass() returns 这个值在 onNavigationDrawerItemSelected() 中。 在 "onNavigationDrawerItemSelected" 中,使用 cl.newInstance() 调用创建了一个新的 HintsMenu。 但是,没有调用 HintsMenu 的 newInstance 方法,而是调用了空的 Constructor。
所以安装了预期的输出
Calling HintsMenu.newInstance()...
HintsMenu.newInstance()
Hintsmenu constructor
我只得到
Calling HintsMenu.newInstance()...
Hintsmenu constructor
如果我这样做
HintsMenu.newInstance()
我得到了预期的输出。 谁能告诉我,我做错了什么?
HintsMenu
上的静态 newInstance()
不是 the newInstance()
method on Class
。您正在调用后者。例如,如果您要将 HintsMenu
上的 newInstance()
重命名为 newMenu()
,并且您将 onNavigationDrawerItemSelected()
实现更改为尝试调用 newMenu()
,您将得到编译错误,因为编译器会抱怨它无法在 Class
.
newMenu()
方法