2012-11-08 35 views
0

林導航到代碼中的其他視圖經歷的iOS開發,但新的Android開發人員,並要求一些新手問題在這裏...從SurfaceView

我想提出一個應用程序,做了很多PNG的和動畫的自定義繪製的並且根本沒有標準的用戶界面元素,我選擇沿着SurfaceView的道路走下去。我還處理了SurfaceView代碼中所觸及的所有檢測。

但我怎麼處理我的SurfaceView代碼中的視圖之間的導航?我如何導航到名爲QuizActivity的活動?在一個「正常」視圖/活動我不喜歡這樣寫道:

Intent intent = new Intent(getBaseContext(), QuizActivity.class); 
startActivity(intent); 

但我沒有從我的SurfaceView中獲得getBaseContext和startActivity,即使我會做這樣的結果在加載多個視圖同一時間?

底線:如何在我的SurfaceView中的代碼中手動實現此導航?

謝謝
瑟倫

回答

2

從表面觀只要致電:

Intent intent = new Intent(getContext(), QuizActivity.class); 
    getContext().startActivity(intent) 

每個視圖都有他們正在運行的上下文的引用,和上下文可以隨時啓動新的活動,服務,獲得資源等

編輯

您surfaceview您包括此:

private SurfaceCallbacks listener; 

    public interface SurfaceCallbacks{ 
     public void onTouch(/* any data you want to pass to the activity*/); 
    } 

    public void registerSurfaceCallbacksListener(SurfaceCallbacks l){ 
     listener = l; 
    } 

    // and then whenever the surface being touched and you want to call something outside of the surface you do: 

    if(listener!=null) 
     listener.onTouch(/* pass the parameters you declared on the interface */); 

並保存你這樣做表面活性:

public ActivityThatHoldsSurface extends Activity implements SurfaceCallbacks{ 

     // that comes form the surface 
     @Override 
     onTouch(/* your parameters */){ 
      // do the navigation stuff 
     } 

     // and immediately after you inflate or instantiate your surface you do: 
     mySurface.registerSurfaceCallbacksListener(ActivityThatHoldsSurface.this); 

    } 

是否有意義???

+0

然後我得到這個錯誤:「E/StrictMode(1502):class com.some.Class; instances = 2; limit = 1」,是因爲我試圖導航回到我導航的活動從到持有SurfaceView的活動?我以前看過這個錯誤,似乎有一些東西讓我瞭解這些活動是如何加載和發佈的,這裏有什麼指針? – Neigaard

+0

我從來沒有見過這個消息,但可能是這個原因。但是,你似乎混合了活動和視圖的概念。即使你的觀點是整個屏幕,它仍然在一個活動。我認爲在這一點上你現在最好的就是退後一步。創建一個接口,讓您的活動持有該視圖實現該接口,並在您的視圖上創建一個公共無效setTouchCallbackListener(TouchInterface l){},以便該活動註冊自己以接收這些回調。如果您不知道這意味着什麼,請告訴我,我會發布一些代碼。 – Budius

+0

Busius,是的,請更新您的答案,如果你有時間,我不知道我在這裏知道你的意思。 – Neigaard