片段的onStop()被ONSTART(后直接调用) - 为什么? [英] Fragment onStop() being called directly after onStart() - WHY?

查看:423
本文介绍了片段的onStop()被ONSTART(后直接调用) - 为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个奇怪的问题,我的应用程序 -

I'm having a strange problem with my app -

一个有一个片段的活动,其中包含一个片段 - 这片段启动一个的AsyncTask的onCreate()和取消的AsyncTask的onStop()。我的问题是因为,虽然我的片段保持运行和放大器;是不是掩盖它的onStop()被调用,几乎是直接的OnCreate()之后。

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()

修改

我已经删除的片段code,我仍然绝对困惑 - 该问题仍然存在!我已经添加了几行记录的:

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()

以上的行为是......莫名其妙。在活动中使用的code是这样的:

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

从code似乎的onStop()被ONSTART后直接叫()。我试着加入了视频下载(1000)在onCreateView(),看看它被称为在同一时间为ONSTART()。输出是一样的 - 这使我beleive的的onStop()被调用/直接从片段创作过程中造成

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

堆栈跟踪上的onStop()分手时: ![在此输入图片说明] [1]

Stacktrace when breaking on onStop(): ![enter image description here][1]

我要去尝试,并附加源$ C ​​$ c和步骤,通过发现问题所在。

I'm going to try and attach the source code and step through to discover where the problem is.

推荐答案

不过不知道是什么,它​​试图做...我研究了从哪里得到源$ C ​​$下参与称为类的的onStop ()和发现源Android的支持,v4.jar自带的SDK中的jar一起打包。

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.

安装这个源后,虽然我很快发现这是不同步的,而我的Andr​​oid支持,v4.jar是完全不同的,以一个随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.

更换同一个封装与SDK的jar解决问题即时,没有的onStop()被调用ONSTART后()。不知道是造成这一点,但最新的版本似乎修复它很轻易什么错误。

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.

这篇关于片段的onStop()被ONSTART(后直接调用) - 为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆