在onStart()之后直接调用片段onStop() - 为什么?

Gra*_*eme 9 lifecycle android

我的应用程序出现了一个奇怪的问题 -

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()
Run Code Online (Sandbox Code Playgroud)

编辑

我从片段中删除了代码,我仍然感到困惑 - 问题仍然存在!我添加了几行日志记录:

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()
Run Code Online (Sandbox Code Playgroud)

上述行为令人困惑.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 ...
Run Code Online (Sandbox Code Playgroud)

这是调试期间的片段:

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()");
}   
Run Code Online (Sandbox Code Playgroud)

EDIT2

从代码看,onSttop()似乎是在onStart()之后直接调用的.我试图通过在onCreateView()中添加Thread.sleep(1000)来查看它是否与onStart()同时被调用.输出是相同的 - 这让我相信onStop()是直接从Fragment创建过程调用/引起的.

EDIT3

打破onStop()时的Stacktrace: 在此输入图像描述

我将尝试附加源代码并逐步解决问题所在.

Gra*_*eme 8

仍然不知道它想要做什么...我研究了在哪里获得了调用onStop()所涉及的类的源代码,并发现android-support-v4.jar的源代码与SDK中的jar.

虽然我很快发现它不同步,并且我的android-support-v4.jar与当前版本的SDK附带的版本大不相同.

使用SDK打包的jar替换jar会立即修复问题,并且在onStart()之后不会调用onStop().不知道是什么错误造成了这个,但最新的版本似乎很容易解决它.