2013-04-20 19 views
0

簡介:我已經做了一個基本活動來擴展我的其他活動。我已經重寫了幾種方法與在函數體可運行,例如:可運行的基本活動

@Override 
    protected void onStop(){ 
     new Handler().postDelayed(new Runnable() 
     { 
      @Override 
      public void run() 
      { 
       BaseActivity.super.onStop(); 
      } 
     }, Fade.fadeDuration); 
    } 

不過,我得到一個SuperNotCalledException當我嘗試運行應用程序。如果我將super.onStop()從runnable中取出,我不會有任何例外。

問題:如何從基本活動中的runnable調用super.onStop而不引發SuperNotCalledException?

附加信息:我想添加淡出某些視圖的淡入淡出。這需要大約700ms,所以我需要延遲700ms的onStop。問題在於,這在每個活動中都很麻煩。我想做一個基本的活動,所以我不必擔心每一個活動的衰落。

+0

也許[this](http://stackoverflow.com/questions/6706280/error-android-app-supernotcalledexception)是適用的。 – TNW 2013-04-20 11:28:46

+0

恐怕不是。 :( – 2013-04-20 11:37:02

+2

你爲什麼要這樣做? – 2013-04-20 11:40:22

回答

0

如果您試圖簡單地延遲執行super.onStop我會使用CountDownLatch

也許是這樣的:

private void CountDownLatch latch; 
private void long latchWait = 10L; // seconds 
private void TimeUnit latchWaitUnit = SECONDS; 

@Override 
protected void onStop(){ 
    try{ 
     this.latch.await(this.latchWait, this.latchWaitUnit); 
    catch(InterruptedException e){ 
     // Handle 
    }finally{ 
     super.onStop(); 
    } 
} 

public void startLatch(long wait){ 
    this.latch = new CountDownLatch(1); 
    this.latchWait = wait; 
} 

public void releaseLatch(){ 
    this.latch.countDown() 
} 

我沒有測試此代碼。

+0

你確定嗎? this.latch.await(this.latchWait,this.latchWaitUnit);會在主線程中運行,不是嗎? – stinepike 2013-04-20 11:59:33

+0

@StinePike是的,你可以用它來延遲'onStop'的運行,而你在另一個線程中運行了某些東西。你究竟想要做什麼? – 2013-04-20 12:06:39

+0

否我只是擔心,如果它會創建ANR崩潰..對不起,我的無知 – stinepike 2013-04-20 12:08:01