是否在UI线程上调用了Android.App.Application活动生命周期回调?

时间:2015-05-22 01:49:01

标签: java android multithreading android-activity xamarin

我正在调查更新我们使用的第三方库(Xamarin.Insights)后开始发生的性能下降。在我调查过程中,我在一次大停顿期间停止了应用程序,该停顿显示隐藏在Android.App.Android.OnActivityDestroyed内的互斥锁。

调用堆栈显示库利用ActivityLifecycleCallbacks接口的已注册实例来跟踪应用程序中的活动生命周期事件。我试图确定是否在UI线程上调用了这些回调。

直觉上我认为它们是在UI线程上调用的,但是我无法找到证实这一点的明确证据。

  • 是否在UI线程上调用ActivityLifecycleCallbacks
  • 我可以提供一个提供此证据的链接吗?

3 个答案:

答案 0 :(得分:10)

确实在UI线程上调用了Android.App.Application.ActivityLifecycleCallbacks的回调方法。

实现Android.App.Application.ActivityLifecycleCallbacks接口并记录回调方法的线程ID,表明线程ID与调用活动的OnCreate方法的线程相同:

OnActivityCreated on thread 1
OnCreate on thread 1
OnActivityStarted on thread 1
OnActivityResumed on thread 1

下面的示例验证了这一点:

[Activity (Label = "UIThreadsCallbacks", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
    protected override void OnCreate (Bundle bundle)
    {
        base.OnCreate (bundle);

        // Set our view from the "main" layout resource
        SetContentView (Resource.Layout.Main);

        Console.WriteLine ("OnCreate on thread " + System.Threading.Thread.CurrentThread.ManagedThreadId);
    }
}

[Application]
public class MyApplication : Application
{
    public MyApplication(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer) { 
    }

    public override void OnCreate ()
    {
        RegisterActivityLifecycleCallbacks(new LifecycleCallbacks());
        base.OnCreate ();
    }
}

public class LifecycleCallbacks : Java.Lang.Object, Android.App.Application.IActivityLifecycleCallbacks
{
    public void OnActivityCreated (Activity activity, Bundle savedInstanceState)
    {
        Console.WriteLine ("OnActivityCreated on thread " + System.Threading.Thread.CurrentThread.ManagedThreadId);
    }

    public void OnActivityDestroyed (Activity activity)
    {
        Console.WriteLine ("OnActivityDestroyed on thread " + System.Threading.Thread.CurrentThread.ManagedThreadId);
    }

    public void OnActivityPaused (Activity activity)
    {
        Console.WriteLine ("OnActivityPaused on thread " + System.Threading.Thread.CurrentThread.ManagedThreadId);
    }

    public void OnActivityResumed (Activity activity)
    {
        Console.WriteLine ("OnActivityResumed on thread " + System.Threading.Thread.CurrentThread.ManagedThreadId);
    }

    public void OnActivitySaveInstanceState (Activity activity, Bundle outState)
    {
        Console.WriteLine ("OnActivitySaveInstanceState on thread " + System.Threading.Thread.CurrentThread.ManagedThreadId);
    }

    public void OnActivityStarted (Activity activity)
    {
        Console.WriteLine ("OnActivityStarted on thread " + System.Threading.Thread.CurrentThread.ManagedThreadId);
    }

    public void OnActivityStopped (Activity activity)
    {
        Console.WriteLine ("OnActivityStopped on thread " + System.Threading.Thread.CurrentThread.ManagedThreadId);
    }
}

答案 1 :(得分:4)

您可以轻松查看称为方法的线程:

  1. 通过记录Thread.currentThread().getName(),如果是主线程,您会看到Main Thread之类的内容。
  2. 通过检查当前线程Looper.getMainLooper() == Looper.getMyLooper()的Looper,如果是主线程,它将是true
  3. 通过在IDE /调试器的堆栈跟踪中为方法设置断点并检查线程名称。

答案 2 :(得分:2)

是的,它确实在UI Thread上调用。 ActivityLifecycleCallbacks由Activity.onXXX()方法调用。例如,以下是Activity.onCreate()的源代码。 getApplication().dispatchActivityCreated(this, savedInstanceState);将调用ActivityLifecycleCallbacks.onActivityCreate()。 所以在UI Thread上调用它。

@MainThread
@CallSuper
protected void onCreate(@Nullable Bundle savedInstanceState) {
    if (DEBUG_LIFECYCLE) Slog.v(TAG, "onCreate " + this + ": " + savedInstanceState);
    if (mLastNonConfigurationInstances != null) {
        mFragments.restoreLoaderNonConfig(mLastNonConfigurationInstances.loaders);
    }
    if (mActivityInfo.parentActivityName != null) {
        if (mActionBar == null) {
            mEnableDefaultActionBarUp = true;
        } else {
            mActionBar.setDefaultDisplayHomeAsUpEnabled(true);
        }
    }
    if (savedInstanceState != null) {
        Parcelable p = savedInstanceState.getParcelable(FRAGMENTS_TAG);
        mFragments.restoreAllState(p, mLastNonConfigurationInstances != null
                ? mLastNonConfigurationInstances.fragments : null);
    }
    mFragments.dispatchCreate();
    getApplication().dispatchActivityCreated(this, savedInstanceState);
    if (mVoiceInteractor != null) {
        mVoiceInteractor.attachActivity(this);
    }
    mCalled = true;
}