我正在调查更新我们使用的第三方库(Xamarin.Insights)后开始发生的性能下降。在我调查过程中,我在一次大停顿期间停止了应用程序,该停顿显示隐藏在Android.App.Android.OnActivityDestroyed
内的互斥锁。
调用堆栈显示库利用ActivityLifecycleCallbacks
接口的已注册实例来跟踪应用程序中的活动生命周期事件。我试图确定是否在UI线程上调用了这些回调。
直觉上我认为它们是在UI线程上调用的,但是我无法找到证实这一点的明确证据。
ActivityLifecycleCallbacks
?答案 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)
您可以轻松查看称为方法的线程:
Thread.currentThread().getName()
,如果是主线程,您会看到Main Thread
之类的内容。Looper.getMainLooper() == Looper.getMyLooper()
的Looper,如果是主线程,它将是true
。答案 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;
}