在 onStart() 之后直接调用 Fragment onStop() - 为什么?
我的应用程序遇到了一个奇怪的问题 -
A 有一个包含片段的片段活动 - 该片段启动 AsyncTask onCreate() 并取消 AsyncTask onStop()。我的问题出现是因为,虽然我的片段保持运行并且没有被掩盖,它的 onStop() 几乎是在 onCreate() 之后立即调用的。
有谁知道如何追踪为什么会发生这种情况?
09-28 11:41:56.785: VERBOSE/SearchFragment1(924): onCreate()
09-28 11:41:56.796: VERBOSE/SearchFragment1(924): onStop()
编辑
我已经从片段中删除了代码,但我仍然感到非常困惑 - 问题仍然存在!我又添加了几行日志记录:
09-28 14:09:00.242: VERBOSE/SearchResultsFragment1(1789): onAttach()
09-28 14:09:00.242: VERBOSE/SearchResultsFragment1(1789): onCreate()
09-28 14:09:00.242: VERBOSE/SearchResultsFragment1(1789): onCreateView()
09-28 14:09:00.242: VERBOSE/SearchResultsFragment1(1789): onActivityCreated()
09-28 14:09:00.242: VERBOSE/SearchResultsFragment1(1789): onStart()
09-28 14:09:00.246: VERBOSE/SearchResultsFragment1(1789): onStop()
09-28 14:09:00.246: VERBOSE/SearchResultsFragment1(1789): onStart()
09-28 14:09:00.246: VERBOSE/SearchResultsFragment1(1789): onResume()
上述行为......令人困惑。 Activity 中使用的代码如下所示:
if(savedInstanceState == null)
{
try {
FragmentTransaction transaction= getSupportFragmentManager().beginTransaction();
Fragment currentFragment= (Fragment)Class.forName(getIntent().getAction()).newInstance();
transaction.replace(R.id.singlePane, currentFragment);
transaction.commit();
} catch ...
这是调试期间的 Fragment:
private static final boolean LOGGING_ENABLED = true;
private static int global_creation_count = 0;
private int local_count = global_creation_count;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
if(LOGGING_ENABLED) Log.v(this.getClass().getSimpleName() + local_count, "onAttach()");
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
global_creation_count+=1;
if(LOGGING_ENABLED) Log.v(this.getClass().getSimpleName() + local_count, "onCreate()");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if(LOGGING_ENABLED) Log.v(this.getClass().getSimpleName() + local_count, "onCreateView()");
return super.onCreateView(inflater, container, savedInstanceState);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if(LOGGING_ENABLED) Log.v(this.getClass().getSimpleName() + local_count,
"onActivityCreated()");
}
@Override
public void onStart() {
super.onStart();
if(LOGGING_ENABLED) Log.v(this.getClass().getSimpleName() + local_count, "onStart()");
}
@Override
public void onResume() {
super.onResume();
if(LOGGING_ENABLED) Log.v(this.getClass().getSimpleName() + local_count, "onResume()");
}
// Fragment is active
@Override
public void onPause() {
super.onPause();
if(LOGGING_ENABLED) Log.v(this.getClass().getSimpleName() + local_count, "onPause()");
}
@Override
public void onStop() {
super.onStop();
if(LOGGING_ENABLED) Log.v(this.getClass().getSimpleName() + local_count, "onStop()");
}
@Override
public void onDestroyView() {
super.onDestroyView();
if(LOGGING_ENABLED) Log.v(this.getClass().getSimpleName() + local_count, "onDestroyView()");
}
@Override
public void onDestroy() {
super.onDestroy();
if(LOGGING_ENABLED) Log.v(this.getClass().getSimpleName() + local_count, "onDestroy()");
}
@Override
public void onDetach() {
super.onDetach();
if(LOGGING_ENABLED) Log.v(this.getClass().getSimpleName() + local_count, "onDetach()");
}
EDIT2
从代码看来,onStop() 是在 onStart() 之后直接调用的。我尝试通过在 onCreateView() 中添加 Thread.sleep(1000) 来查看它是否与 onStart() 同时调用。输出是相同的 - 这让我相信 onStop() 是直接从片段创建过程中调用/引起的。
EDIT3
在 onStop() 上中断时的堆栈跟踪:
我将尝试附加源代码并逐步找出问题所在。
I'm having a strange problem with my app -
A have a Fragment Activity which contains a fragment - This fragment starts an AsyncTask onCreate() and cancels the AsyncTask onStop(). My problem arises because, although my Fragment stays running & isn't obscured it's onStop() is called almost directly after it's onCreate().
Does anyone know how to trace why this would be happening?
09-28 11:41:56.785: VERBOSE/SearchFragment1(924): onCreate()
09-28 11:41:56.796: VERBOSE/SearchFragment1(924): onStop()
EDIT
I've removed the code from the Fragment and I'm still absolutely baffled - The problem persists! I've added a few more lines of logging:
09-28 14:09:00.242: VERBOSE/SearchResultsFragment1(1789): onAttach()
09-28 14:09:00.242: VERBOSE/SearchResultsFragment1(1789): onCreate()
09-28 14:09:00.242: VERBOSE/SearchResultsFragment1(1789): onCreateView()
09-28 14:09:00.242: VERBOSE/SearchResultsFragment1(1789): onActivityCreated()
09-28 14:09:00.242: VERBOSE/SearchResultsFragment1(1789): onStart()
09-28 14:09:00.246: VERBOSE/SearchResultsFragment1(1789): onStop()
09-28 14:09:00.246: VERBOSE/SearchResultsFragment1(1789): onStart()
09-28 14:09:00.246: VERBOSE/SearchResultsFragment1(1789): onResume()
The above behaviour is... baffling. The code used in the Activity looks like this:
if(savedInstanceState == null)
{
try {
FragmentTransaction transaction= getSupportFragmentManager().beginTransaction();
Fragment currentFragment= (Fragment)Class.forName(getIntent().getAction()).newInstance();
transaction.replace(R.id.singlePane, currentFragment);
transaction.commit();
} catch ...
This is the Fragment as it stands during debug:
private static final boolean LOGGING_ENABLED = true;
private static int global_creation_count = 0;
private int local_count = global_creation_count;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
if(LOGGING_ENABLED) Log.v(this.getClass().getSimpleName() + local_count, "onAttach()");
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
global_creation_count+=1;
if(LOGGING_ENABLED) Log.v(this.getClass().getSimpleName() + local_count, "onCreate()");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if(LOGGING_ENABLED) Log.v(this.getClass().getSimpleName() + local_count, "onCreateView()");
return super.onCreateView(inflater, container, savedInstanceState);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if(LOGGING_ENABLED) Log.v(this.getClass().getSimpleName() + local_count,
"onActivityCreated()");
}
@Override
public void onStart() {
super.onStart();
if(LOGGING_ENABLED) Log.v(this.getClass().getSimpleName() + local_count, "onStart()");
}
@Override
public void onResume() {
super.onResume();
if(LOGGING_ENABLED) Log.v(this.getClass().getSimpleName() + local_count, "onResume()");
}
// Fragment is active
@Override
public void onPause() {
super.onPause();
if(LOGGING_ENABLED) Log.v(this.getClass().getSimpleName() + local_count, "onPause()");
}
@Override
public void onStop() {
super.onStop();
if(LOGGING_ENABLED) Log.v(this.getClass().getSimpleName() + local_count, "onStop()");
}
@Override
public void onDestroyView() {
super.onDestroyView();
if(LOGGING_ENABLED) Log.v(this.getClass().getSimpleName() + local_count, "onDestroyView()");
}
@Override
public void onDestroy() {
super.onDestroy();
if(LOGGING_ENABLED) Log.v(this.getClass().getSimpleName() + local_count, "onDestroy()");
}
@Override
public void onDetach() {
super.onDetach();
if(LOGGING_ENABLED) Log.v(this.getClass().getSimpleName() + local_count, "onDetach()");
}
EDIT2
From the code it seems that the onStop() is being called directly after onStart(). I've tried to see if it's being called at the same time as onStart() by adding a Thread.sleep(1000) in onCreateView(). The output is the same - which makes me beleive the onStop() is being called/caused directly from Fragment creation process.
EDIT3
Stacktrace when breaking on onStop():
I'm going to try and attach the source code and step through to discover where the problem is.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
仍然不知道它想做什么...我研究了在哪里获取涉及调用 onStop() 的类的源代码,发现 android-support-v4.jar 的源代码与SDK 中的 jar 包。
在附加此源代码后,我很快发现它不同步,并且我的 android-support-v4.jar 与当前版本的 SDK 附带的版本有很大不同。
将jar替换为SDK自带的jar即可解决问题,并且onStart()之后不再调用onStop()。不确定是什么错误导致了这个问题,但最新版本似乎很容易修复它。
Still don't know what it was trying to do... I researched on where to get the source code for the class's involved in called onStop() and found that the source for android-support-v4.jar comes packaged along with the jar in the SDK.
After attaching this source though I soon found it was out of sync and that my android-support-v4.jar was vastly different to the one shipped with the current version of the SDK.
Replacing the jar with the one packaged with the SDK fixes the problem instantly and no onStop() is called after onStart(). Not sure what bug was causing this but the latest version seems to fix it quite handily.