2014-10-07 43 views
0

我無法可靠地退出我的應用程序中的活動。當我按下設備上的「後退」按鈕或者我從按鈕單擊呼叫finish()時,活動顯然是隨機重新啓動。onPreated()onPause()和onStop()之間的活動

示例活動:

[Activity(Label = "abc", ParentActivity = typeof(MainActivity))] 
public class ImpressumActivity : Activity 
{ 
    private static int _instancecounter; 
    private int _thisinstance; 
    protected override void OnCreate(Bundle bundle) 
    { 
     _thisinstance = _instancecounter++; 
     Console.WriteLine("Creating instance {0}", _thisinstance); 
     base.OnCreate(bundle); 
     SetContentView(Resource.Layout.Impressum); 
     Console.WriteLine("Created instance {0}", _thisinstance); 
    } 

    public override void Finish() 
    { 
     Console.WriteLine("Finishing instance {0}", _thisinstance); 
     base.Finish(); 
     Console.WriteLine("Finished instance {0}", _thisinstance); 
    } 

    protected override void OnStop() 
    { 
     Console.WriteLine("Stopping instance {0}", _thisinstance); 
     base.OnStop(); 
     Console.WriteLine("Stopped instance {0}", _thisinstance); 
    } 

    protected override void OnPause() 
    { 
     Console.WriteLine("Pausing instance {0}", _thisinstance); 
     base.OnPause(); 
     Console.WriteLine("Paused instance {0}", _thisinstance); 
    } 

    protected override void OnDestroy() 
    { 
     Console.WriteLine("Destroying instance {0}", _thisinstance); 
     base.OnDestroy(); 
     Console.WriteLine("Destroyed instance {0}", _thisinstance); 
    } 
} 

現在,我在MainActivity開始十一時二十分五十秒通過impressumButton.Click += delegate { StartActivity(typeof(ImpressumActivity)); };的activiy,然後我按後退按鈕,在11時20分53秒。活動「保持」在屏幕上;實際上它得到了重新創建,我可以從輸出中看到:

10-07 11:20:50.255 I/mono-stdout(6149): Creating instance 0 
10-07 11:20:50.275 I/mono-stdout(6149): Created instance 0 
10-07 11:20:50.275 D/Activity(6149): setTransGradationMode to true 
10-07 11:20:53.535 I/mono-stdout(6149): Finishing instance 0 
10-07 11:20:53.555 I/mono-stdout(6149): Finished instance 0 
10-07 11:20:53.565 I/mono-stdout(6149): Pausing instance 0 
10-07 11:20:53.585 I/mono-stdout(6149): Paused instance 0 
10-07 11:20:53.605 I/mono-stdout(6149): Creating instance 1 
10-07 11:20:53.625 I/mono-stdout(6149): Created instance 1 
10-07 11:20:53.625 D/Activity(6149): setTransGradationMode to true 
10-07 11:20:54.015 I/mono-stdout(6149): Stopping instance 0 
10-07 11:20:54.015 I/mono-stdout(6149): Stopped instance 0 
10-07 11:20:54.015 I/mono-stdout(6149): Destroying instance 0 
10-07 11:20:54.015 I/mono-stdout(6149): Destroyed instance 0 

我在其他活動中具有相同的行爲。我試過NoHistory=true,這有幫助,但打破了其他的事情(我想要在更復雜的活動中的通話記錄),有和沒有ParentActivity = ...,但我卡住了。

更新:這是一個可以重現的MainActivity()行爲: ():請注意,我描述的行爲並不總是顯示。當我第二次進入ImpressumActivity時,似乎出現主要是,或者更具體地說,我需要在第n次進入活動時按「返回」n(儘管它每次都被銷燬!)

[Activity(Label = "@string/MainHL", MainLauncher = true, Icon = "@drawable/icon")] 
public class MainActivity : Activity 
{ 
    protected override void OnCreate(Bundle bundle) 
    { 
     base.OnCreate(bundle); 
     SetContentView(Resource.Layout.Main); 
    } 

    protected override void OnResume() 
    { 
     base.OnResume(); 
     var impressumButton = FindViewById<Button>(Resource.Id.impressum); 
     impressumButton.Click += delegate { StartActivity(typeof (ImpressumActivity)); }; 
    } 
} 
+0

在一個空白的Android項目中,並實現默認按鈕的單擊事件,它的工作原理與之相同。聽起來像某種導航問題。 – 2014-10-07 10:56:49

+0

任何想法可能導致導航問題? – baeuchle 2014-10-07 13:47:10

+0

是所有的代碼?你不重寫OnBackPressed或類似的東西?沒有看到所有的代碼很難說。 – 2014-10-07 13:58:32

回答

1

確實,這是問題所在。每次OnResume發生線路點擊事件,但您不釋放它。它會導致點擊後創建許多新的活動實例。而不是在OnCreate中點擊線路事件,你會沒事的。

+0

哦,所以它是'* .Click。+ = ...'中的'+'。因此,在五個OnResume()之後,我有五個處理程序連接到一個按鈕? (明天我會試試看並報告回) – baeuchle 2014-10-07 17:42:12

+0

是的,除非活動重新創建,否則就是這種情況。 + =添加處理程序, - =刪除它。 – 2014-10-07 17:46:14

+0

teriffic,現在它工作。謝謝一堆! – baeuchle 2014-10-08 06:54:09

相關問題