在调用finish()之后重新创建活动

时间:2013-04-26 11:47:52

标签: android android-activity

我正在开发一个voip应用程序,它主要使用主Activity(XTabsContainer)和Service(NativeService)。始终使用标志FLAG_ACTIVITY_SINGLE_TOP和FLAG_ACTIVITY_NEW_TASK启动主Activity(即来自SplashScreen活动)。

在主Activity中我有一个OptionsMenu,只有一个项目用于退出应用程序,具有以下功能:

public void exit(){
    Log.d(TAG, "exit()");
    mExitThread = new ExitThread();
    mExitThread.start();
    this.finish();
}

ExitThread()只关闭一些东西并终止服务:

private class ExitThread extends Thread
{
    public void run() {
        Log.d(TAG, "Starting Unregistration thread");
        INgnSipService sipService = Engine.getInstance().getSipService();
        int count = 0;
        if (sipService.isRegistered())
        {
            sipService.unRegister();
            while (count <= NativeService.mTimeout && sipService.isRegistered())
            {
                try {
                    Thread.sleep(1000);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                count++;
            }
            // Exit for timeout
            if (count > NativeService.mTimeout)
                Log.w(TAG, "Exiting Unregistration thread for TIMEOUT");
            else
                Log.d(TAG, "Exiting Unregistration thread");
        }
        if (!Engine.getInstance().stop()) {
            Log.e(TAG, "Failed to stop engine");
        }   
    }   
}

比finish()应该关闭主Activity。

随机发生的事情是,在调用finish()之后,主Activity再次启动,我不明白为什么,所以我需要再次点击OptionsMenu中的“Exit”按钮实际退出来自应用程序。

发生这种情况时跟踪日志。特别是第一次调用exit()函数是在04-26 10:29:21.580。

提前感谢您的帮助。

## Raise the app in foreground

04-26 10:29:16.020: D/it.domain.myapp.XTabsContainer(32540): onResume()
04-26 10:29:16.020: D/it.domain.myapp.XTabsContainer(32540): TabsFragmentActivity handleAction(): ACTION_NOTIFY_HIDDEN_CALL value=false
04-26 10:29:18.840: W/ResourceType(32540): Failure getting entry for 0x010802c1 (t=7 e=705) in package 0 (error -75)
04-26 10:29:19.075: D/dalvikvm(32540): GC_CONCURRENT freed 1601K, 26% free 14639K/19527K, paused 22ms+4ms, total 171ms

## Press Exit button in the OptionsMenu

04-26 10:29:21.580: D/it.domain.myapp.XTabsContainer(32540): exit()
04-26 10:29:21.605: D/it.domain.myapp.XTabsContainer(32540): Starting Unregistration thread
04-26 10:29:21.605: D/org.doubango.ngn.services.impl.NgnConfigurationService(32540): stopping...
04-26 10:29:21.615: D/org.doubango.ngn.services.impl.NgnHistoryService(32540): Stopping
04-26 10:29:21.615: D/org.doubango.ngn.services.impl.NgnStorageService(32540): stopping...
04-26 10:29:21.615: D/org.doubango.ngn.services.impl.NgnContactService(32540): stopping...
04-26 10:29:21.620: D/org.doubango.ngn.services.impl.NgnContactService(32540): Observer Looper exit()
04-26 10:29:21.620: D/org.doubango.ngn.services.impl.NgnSipService(32540): stopping...
04-26 10:29:21.870: D/org.doubango.ngn.services.impl.NgnSipService(32540): GSM Observer stopped...
04-26 10:29:21.870: D/org.doubango.ngn.services.impl.NgnSoundService(32540): stopping...
04-26 10:29:21.870: D/org.doubango.ngn.services.impl.NgnNetworkService(32540): Stopping...
04-26 10:29:21.970: D/it.domain.myapp.NativeService(32540): onDestroy()
04-26 10:29:21.970: D/org.doubango.ngn.NgnNativeService(32540): onDestroy()

## Here the main Activity is started again

04-26 10:29:21.980: D/it.domain.myapp.XTabsContainer(32540): XTabsContainer()
04-26 10:29:21.980: D/it.domain.myapp.XTabsContainer(32540): onCreate()
04-26 10:29:21.980: D/org.doubango.ngn.services.impl.NgnNetworkService(32540): acquireNetworkLock()
04-26 10:29:21.985: D/org.doubango.ngn.services.impl.NgnNetworkService(32540): netType=1 and netSubType=0
04-26 10:29:22.000: D/it.domain.myapp.XTabsContainer(32540): initialiseTabHost()
04-26 10:29:22.005: D/it.domain.myapp.TabFragRecents(32540): TabFragRecents()
04-26 10:29:22.005: D/it.domain.myapp.TabFragRecents(32540): onCreateView()
04-26 10:29:22.015: D/AbsListView(32540): Get MotionRecognitionManager
04-26 10:29:22.015: D/it.domain.myapp.TabFragRecents(32540): onActivityCreated()
04-26 10:29:22.015: D/it.domain.myapp.ContactsListAdapter(32540): ContactsListAdapter()
04-26 10:29:22.015: D/it.domain.myapp.TabFragRecents(32540): MyRecentsListAdapter()
04-26 10:29:22.020: D/it.domain.myapp.TabFragRecents(32540): onStart()
04-26 10:29:22.030: D/it.domain.myapp.XTabsContainer(32540): BadgeNumberUpdate()
04-26 10:29:22.030: D/it.domain.myapp.XTabsContainer(32540): BadgeNumberUpdate(): SIP NOT REGISTERED
04-26 10:29:22.155: D/it.domain.myapp.XTabsContainer(32540): onResume()
04-26 10:29:22.155: D/it.domain.myapp.XTabsContainer(32540): TabsFragmentActivity handleAction(): ACTION_NOTIFY_HIDDEN_CALL value=false
04-26 10:29:22.395: E/SpannableStringBuilder(32540): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
04-26 10:29:22.395: E/SpannableStringBuilder(32540): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length

## Here the previous main Activity is actually destroyed and the new-created main Activity shows up

04-26 10:29:22.395: D/it.domain.myapp.XTabsContainer(32540): onDestroy
04-26 10:29:22.410: D/it.domain.myapp.TabFragRecents(32540): onDestroy

## The new-created main Activity continues its life

04-26 10:29:30.635: D/it.domain.myapp.TabFragContacts(32540): TabFragContacts()
04-26 10:29:30.635: D/it.domain.myapp.TabFragContacts(32540): onCreateView()
04-26 10:29:30.670: D/AbsListView(32540): Get MotionRecognitionManager
04-26 10:29:30.675: D/it.domain.myapp.TabFragContacts(32540): onActivityCreated()
04-26 10:29:30.675: D/it.domain.myapp.TabFragContacts(32540): Pressed button: 2131296425
04-26 10:29:30.680: D/it.domain.myapp.TabFragContacts(32540): onResume()
04-26 10:29:30.695: E/SensorManager(32540): thread start
04-26 10:29:30.700: D/SensorManager(32540): registerListener :: handle = 0  name= K3DH Acceleration Sensor delay= 200000 Trklfufi 9 budiwrd5mrfo5WirfulblrwuFmfulTrklfufi$KfukwiFmfulTrklfufiRvht@,+*a):8(
04-26 10:29:30.790: D/it.domain.myapp.ContactsListAdapter(32540): ContactsListAdapter()
04-26 10:29:30.795: D/it.domain.myapp.TabFragContacts(32540): updateSections()
04-26 10:29:30.855: E/it.domain.myapp.TabFragContacts(32540): onPostExecute(): Exception: java.lang.NullPointerException
04-26 10:29:30.855: E/SpannableStringBuilder(32540): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
04-26 10:29:30.855: E/SpannableStringBuilder(32540): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
04-26 10:29:30.860: E/SpannableStringBuilder(32540): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
04-26 10:29:30.860: E/SpannableStringBuilder(32540): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
04-26 10:29:35.365: D/it.domain.myapp.XTabsContainer(32540): Recents Tab Pressed!
04-26 10:29:35.365: D/SensorManager(32540): unregisterListener:: Trklfufi 9 budiwrd5mrfo5WirfulblrwuFmfulTrklfufi$KfukwiFmfulTrklfufiRvht@,+*a):8(
04-26 10:29:35.370: D/Sensors(32540): Remain listener = Sending .. normal delay 200ms
04-26 10:29:35.370: I/Sensors(32540): sendDelay --- 200000000
04-26 10:29:35.370: D/SensorManager(32540): JNI - sendDelay
04-26 10:29:35.370: I/SensorManager(32540): Set normal delay = true
04-26 10:29:35.380: D/it.domain.myapp.TabFragRecents(32540): onResume()
04-26 10:29:35.380: D/it.domain.myapp.TabFragRecents(32540): updateSections()
04-26 10:29:35.380: D/it.domain.myapp.TabFragRecents(32540): sendActionMsg()
04-26 10:29:35.380: D/it.domain.myapp.TabFragRecents(32540): sendActionMsg(): SIP NOT REGISTERED
04-26 10:29:35.475: W/IInputConnectionWrapper(32540): getSelectedText on inactive InputConnection
04-26 10:29:35.505: W/IInputConnectionWrapper(32540): setComposingText on inactive InputConnection
04-26 10:29:35.505: W/IInputConnectionWrapper(32540): getExtractedText on inactive InputConnection
04-26 10:29:37.030: E/SpannableStringBuilder(32540): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
04-26 10:29:37.030: E/SpannableStringBuilder(32540): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
04-26 10:29:38.950: D/it.domain.myapp.FragmentServices(32540): onCreateView()
04-26 10:29:38.970: D/AbsListView(32540): Get MotionRecognitionManager
04-26 10:29:38.970: D/it.domain.myapp.FragmentServices(32540): buildData()
04-26 10:29:38.970: D/it.domain.myapp.FragmentServices(32540): putData()
04-26 10:29:38.970: D/it.domain.myapp.FragmentServices(32540): putData()
04-26 10:29:38.970: D/it.domain.myapp.FragmentServices(32540): putData()
04-26 10:29:38.975: D/it.domain.myapp.FragmentServices(32540): onResume()
04-26 10:29:39.095: E/SpannableStringBuilder(32540): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
04-26 10:29:39.095: E/SpannableStringBuilder(32540): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
04-26 10:29:41.715: E/SpannableStringBuilder(32540): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
04-26 10:29:41.720: E/SpannableStringBuilder(32540): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
04-26 10:29:47.700: E/SpannableStringBuilder(32540): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
04-26 10:29:47.700: E/SpannableStringBuilder(32540): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
04-26 10:29:49.350: D/it.domain.myapp.ViewInformation(32540): ViewInformation()
04-26 10:29:49.400: E/SpannableStringBuilder(32540): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
04-26 10:29:49.400: E/SpannableStringBuilder(32540): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
04-26 10:29:49.495: E/SpannableStringBuilder(32540): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
04-26 10:29:49.495: E/SpannableStringBuilder(32540): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
04-26 10:30:13.320: D/it.domain.myapp.XTabsContainer(32540): onResume()
04-26 10:30:13.320: D/it.domain.myapp.XTabsContainer(32540): TabsFragmentActivity handleAction(): ACTION_NOTIFY_HIDDEN_CALL value=false
04-26 10:30:13.435: E/SpannableStringBuilder(32540): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
04-26 10:30:13.435: E/SpannableStringBuilder(32540): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
04-26 10:30:16.050: W/ResourceType(32540): Failure getting entry for 0x010802c1 (t=7 e=705) in package 0 (error -75)

## Here I pressed the Exit button the second time

04-26 10:30:16.995: D/it.domain.myapp.XTabsContainer(32540): exit()
04-26 10:30:17.030: D/it.domain.myapp.XTabsContainer(32540): Starting Unregistration thread
04-26 10:30:17.750: D/it.domain.myapp.XTabsContainer(32540): onDestroy
04-26 10:30:17.765: D/it.domain.myapp.TabFragRecents(32540): onDestroy
04-26 10:30:17.770: D/it.domain.myapp.TabFragContacts(32540): onDestroy()

1 个答案:

答案 0 :(得分:0)

您正在使用Handler.post(),这意味着ExitThread中的代码实际上并未在另一个线程中运行,而是在主(UI)线程上运行。你的ExitThread中有一个循环。这不适合在UI线程上运行,因为它会阻止UI。 Android最终会因ANR(应用程序无响应)错误而终止此活动。

您需要在单独的线程中实际运行ExitThread中的内容。而不是:

mHandler.post(new ExitThread());

做的:

new ExitThread().start();