我如何知道 onCreateView 已从外部 class 调用?
How can I know that onCreateView has been called from an outer class?
我真的很好奇如何确定(从外部class)片段的onCreateView()
是否已经被调用。我搜索过类似的问题,但发现 none.
例如,fragment.isAdded()
是一个好的指标吗?
我的第一个想法只是 fragment.getView() != null
,但我不是 100% 确定它看起来是否可靠,而且我也有点不愿意使用它(没有特别的原因,我只是倾向于避免无效检查)。我很乐意找到解决方法。我的建议:
已添加()
Return true if the fragment is currently added to its activity.
这条线在我看来很含糊; 已添加 未附加,但也未创建。它可能指的是 FragmentTransaction.add()
(这在语义上是错误的,因为您可以将 <fragment>
卡在布局中而不必调用 add
或 replace
)。
不过,FragmentTransaction.add()
documentation 没有提供任何信息,也没有让您思考 added -> created
。我会说不。
isVisible()
Return true if the fragment is currently visible to the user. This means it: (1) has been added, (2) has its view attached to the window, and (3) is not hidden.
从某种意义上说,isVisible() -> isCreated
看起来不错,但第三个选项使其成为 isCreated != isVisible
。我只是想到视图分页器中的片段:并非所有片段都是可见的,但是当前可见片段附近的片段被添加、创建并处于活动状态,您可以调用它们的方法。但对他们来说,isVisible() == false
。这有点太严格了。
isInLayout()
Return true if the layout is included as part of an activity view
hierarchy via the < fragment> tag. This will always be true when
fragments are created through the < fragment> tag, except in the case
where an old fragment is restored from a previous state and it does
not appear in the layout of the current state.
我认为这不适用于此。
getView() != null
Returns
The fragment's root view, or null if it has no layout.
这看起来仍然是唯一的解决方案。我只想确认一下。
执行回调
..被称为onCreateView()
,或者更好的是,onViewCreated()
。但是:
- 我不需要在创建片段后立即调用某些东西(你为什么需要那个?),我需要在给定时间检查一些东西;
- 应该定义相反的东西,比如 onViewNotAvailableAnymore(),以使检查始终有意义;
- 我看不出这与
getView != null
有何不同或更好。
Does Fragment.isAdded() imply that onCreateView has been called?
是
isAdded() Return 如果该片段当前已添加到其 activity,则为真。 (隐式调用 onCreateView()
)。
Does Fragment.isAdded() imply that onCreateView has been called?
没有!! 没有!! 暂停 NOOOOOOO00000000000!!!!!
SIr
Fragment.isAdded()
通知您的 Fragment
已添加到您的 Activity
,故事结束。
FragmentTransaction
中的 add()
方法有 3 种不同的方法,所有方法都将 Fragment
添加到 Activity
中,另外两个更进一步创建您的 Fragment
s View
并借助 LayoutInflater
将其附加到 Parent
ViewGroup
,前提是您的第一个参数不是 0 (id != 0)
要检查 onCreateView()
是否已被调用,您需要覆盖 onViewCreated()
.
getView()
将始终 return 空,除非 onCreateView()
完成
您的解决方案是检查 Fragment.isVisible()
仅供参考:我在文档中看到的没有任何问题。很清楚先生。
希望我是清醒的先生
如果片段 returns 上的 isAdded()
为真,并不意味着 onCreateView()
已被调用。事实上,isAdded returns 即使在 onAttach 回调期间也是如此,即在 onCreate() 之前调用。
我会继续扩展 Fragment class 并添加一个 public 方法,您可以使用该方法从自定义 Fragment Class.
外部到达
调用onCreateView()
时,你可以设置一个布尔值true,根据你的架构,你可以将它设置为false 在 onPause()
或 onStop()
或 onDestroyView()
或 onDestroy()
或 onDetach()
时再次返回,由您决定。
我不认为你在问题中提到的方法会提供你所需要的东西。
可以使用界面完成。创建一个接口 OnCreateViewListerner
public interface OnViewCreatedListener
{
void onCreateCalled();
}
在您的 Fragment 中创建一个 OnViewCreatedListener 的静态对象,并在您的外部 class 中对其进行初始化,并且外部 class 实现此接口,如
public class CustomClass implements OnViewCreatedListener{
@Override
public void onCreateCalled() {
}
public void initializeInterface()
{
FragmentTest.listerner = this;
}
.....
}
然后覆盖您的 onCreateView() 方法并编写此
public class FragmentTest extends Fragment{
public static OnViewCreatedListener listerner;
View mView;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// if (container == null)
if(listerner!= null)
{
listerner.onCreateCalled();
}
mView = inflater.inflate(R.layout.fragment_product, container, false);
return mView;
}
}
希望对您有所帮助。
请像我一样考虑这种方法:
定义一个 Interface
在你的片段中说:
public Interface IDoSomething{
void intimateAfterOnCreateView();
}
现在,在片段的 onStart()
中调用此方法,因为根据生命周期,此方法将在 onCreateView()
之后调用。
在这个片段之外,只需实现 IDoSomething,您将自动获得一个重写方法(intimateAfterOnCreateView()
)。
现在这个方法的执行会显示onCreateView()
已经被调用了。
我只是想分享我的知识,可能会有帮助。
假设
- 您对
Fragment
是否可见不感兴趣
- 您只想知道
onCreateView(...)
方法是否已被 Android 框架调用
- 你需要使用现有的片段API方法来找出
然后使用
getView() != null
前提是您膨胀布局和 return onCreateView(...)
中的 View
。
非 Fragment-API 方法是在 onCreate(...)
中添加回调,然后在 onCreateView()
或更高版本(在生命周期中)方法中调用它。
How can I know that onCreateView has been called from an outer class?
您需要在片段中创建接口并在容器中实现它 activity(比如 MainActivity)。
1.首先在你的片段中创建一个接口:
// Container activity must implement this interface
public interface OnCreateViewCalledListener {
void OnCreateViewCalled(String someData);
}
2。接下来在容器 activity 中实现接口(假设它是 MainActivity)并调用它的方法:
public class MainActivity extends AppCompatActivity implements
YourFragment.OnCreateViewCalledListener {
...
@Override
public void OnCreateViewCalled(String someData) {
Toast.makeText(MainActivity.this, "OnCreateView was called and passed " + someData)
}
3。然后需要检查MainActivity是否实现了接口回调(这一步对于使其正常工作至关重要):
//Using onAttach method to check that activity has implemented callbacks
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// Make sure that container activity has implemented
// the callback interface. If not, it throws an exception
try {
mCallback = (OnCreateViewCalledListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnCreateViewCalledListener");
}
}
4.最后你需要在 onCreateView 中触发回调:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mCallback.OnCreateViewCalled("Some useful data");
...
}
就是这样!
编辑:
要让其他 class 知道调用了 onCreateView,请在 MainActivity 中使用 onCreateViewCalled() 回调(例如使用另一个接口在其他 class 中触发回调)。
也不是强制将数据传递到 OnCreateViewCalled()
我真的很好奇如何确定(从外部class)片段的onCreateView()
是否已经被调用。我搜索过类似的问题,但发现 none.
例如,fragment.isAdded()
是一个好的指标吗?
我的第一个想法只是 fragment.getView() != null
,但我不是 100% 确定它看起来是否可靠,而且我也有点不愿意使用它(没有特别的原因,我只是倾向于避免无效检查)。我很乐意找到解决方法。我的建议:
已添加()
Return true if the fragment is currently added to its activity.
这条线在我看来很含糊; 已添加 未附加,但也未创建。它可能指的是 FragmentTransaction.add()
(这在语义上是错误的,因为您可以将 <fragment>
卡在布局中而不必调用 add
或 replace
)。
不过,FragmentTransaction.add()
documentation 没有提供任何信息,也没有让您思考 added -> created
。我会说不。
isVisible()
Return true if the fragment is currently visible to the user. This means it: (1) has been added, (2) has its view attached to the window, and (3) is not hidden.
从某种意义上说,isVisible() -> isCreated
看起来不错,但第三个选项使其成为 isCreated != isVisible
。我只是想到视图分页器中的片段:并非所有片段都是可见的,但是当前可见片段附近的片段被添加、创建并处于活动状态,您可以调用它们的方法。但对他们来说,isVisible() == false
。这有点太严格了。
isInLayout()
Return true if the layout is included as part of an activity view hierarchy via the < fragment> tag. This will always be true when fragments are created through the < fragment> tag, except in the case where an old fragment is restored from a previous state and it does not appear in the layout of the current state.
我认为这不适用于此。
getView() != null
Returns The fragment's root view, or null if it has no layout.
这看起来仍然是唯一的解决方案。我只想确认一下。
执行回调
..被称为onCreateView()
,或者更好的是,onViewCreated()
。但是:
- 我不需要在创建片段后立即调用某些东西(你为什么需要那个?),我需要在给定时间检查一些东西;
- 应该定义相反的东西,比如 onViewNotAvailableAnymore(),以使检查始终有意义;
- 我看不出这与
getView != null
有何不同或更好。
Does Fragment.isAdded() imply that onCreateView has been called?
是
isAdded() Return 如果该片段当前已添加到其 activity,则为真。 (隐式调用 onCreateView()
)。
Does Fragment.isAdded() imply that onCreateView has been called?
没有!! 没有!! 暂停 NOOOOOOO00000000000!!!!!
SIr
Fragment.isAdded()
通知您的 Fragment
已添加到您的 Activity
,故事结束。
FragmentTransaction
中的 add()
方法有 3 种不同的方法,所有方法都将 Fragment
添加到 Activity
中,另外两个更进一步创建您的 Fragment
s View
并借助 LayoutInflater
将其附加到 Parent
ViewGroup
,前提是您的第一个参数不是 0 (id != 0)
要检查 onCreateView()
是否已被调用,您需要覆盖 onViewCreated()
.
getView()
将始终 return 空,除非 onCreateView()
完成
您的解决方案是检查 Fragment.isVisible()
仅供参考:我在文档中看到的没有任何问题。很清楚先生。
希望我是清醒的先生
如果片段 returns 上的 isAdded()
为真,并不意味着 onCreateView()
已被调用。事实上,isAdded returns 即使在 onAttach 回调期间也是如此,即在 onCreate() 之前调用。
我会继续扩展 Fragment class 并添加一个 public 方法,您可以使用该方法从自定义 Fragment Class.
外部到达调用onCreateView()
时,你可以设置一个布尔值true,根据你的架构,你可以将它设置为false 在 onPause()
或 onStop()
或 onDestroyView()
或 onDestroy()
或 onDetach()
时再次返回,由您决定。
我不认为你在问题中提到的方法会提供你所需要的东西。
可以使用界面完成。创建一个接口 OnCreateViewListerner
public interface OnViewCreatedListener
{
void onCreateCalled();
}
在您的 Fragment 中创建一个 OnViewCreatedListener 的静态对象,并在您的外部 class 中对其进行初始化,并且外部 class 实现此接口,如
public class CustomClass implements OnViewCreatedListener{
@Override
public void onCreateCalled() {
}
public void initializeInterface()
{
FragmentTest.listerner = this;
}
.....
}
然后覆盖您的 onCreateView() 方法并编写此
public class FragmentTest extends Fragment{
public static OnViewCreatedListener listerner;
View mView;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// if (container == null)
if(listerner!= null)
{
listerner.onCreateCalled();
}
mView = inflater.inflate(R.layout.fragment_product, container, false);
return mView;
}
}
希望对您有所帮助。
请像我一样考虑这种方法:
定义一个 Interface
在你的片段中说:
public Interface IDoSomething{
void intimateAfterOnCreateView();
}
现在,在片段的 onStart()
中调用此方法,因为根据生命周期,此方法将在 onCreateView()
之后调用。
在这个片段之外,只需实现 IDoSomething,您将自动获得一个重写方法(intimateAfterOnCreateView()
)。
现在这个方法的执行会显示onCreateView()
已经被调用了。
我只是想分享我的知识,可能会有帮助。
假设
- 您对
Fragment
是否可见不感兴趣 - 您只想知道
onCreateView(...)
方法是否已被 Android 框架调用 - 你需要使用现有的片段API方法来找出
然后使用
getView() != null
前提是您膨胀布局和 return onCreateView(...)
中的 View
。
非 Fragment-API 方法是在 onCreate(...)
中添加回调,然后在 onCreateView()
或更高版本(在生命周期中)方法中调用它。
How can I know that onCreateView has been called from an outer class?
您需要在片段中创建接口并在容器中实现它 activity(比如 MainActivity)。
1.首先在你的片段中创建一个接口:
// Container activity must implement this interface
public interface OnCreateViewCalledListener {
void OnCreateViewCalled(String someData);
}
2。接下来在容器 activity 中实现接口(假设它是 MainActivity)并调用它的方法:
public class MainActivity extends AppCompatActivity implements
YourFragment.OnCreateViewCalledListener {
...
@Override
public void OnCreateViewCalled(String someData) {
Toast.makeText(MainActivity.this, "OnCreateView was called and passed " + someData)
}
3。然后需要检查MainActivity是否实现了接口回调(这一步对于使其正常工作至关重要):
//Using onAttach method to check that activity has implemented callbacks
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// Make sure that container activity has implemented
// the callback interface. If not, it throws an exception
try {
mCallback = (OnCreateViewCalledListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnCreateViewCalledListener");
}
}
4.最后你需要在 onCreateView 中触发回调:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mCallback.OnCreateViewCalled("Some useful data");
...
}
就是这样!
编辑:
要让其他 class 知道调用了 onCreateView,请在 MainActivity 中使用 onCreateViewCalled() 回调(例如使用另一个接口在其他 class 中触发回调)。 也不是强制将数据传递到 OnCreateViewCalled()