2010-08-09 111 views
0

我正在實施一個簡單的應用程序。我需要根據活動的狀態開始一項活動。讓我拿着一個按鈕來開始活動。 1.如果活動未開始,我需要啓動XYZ活動。 2.如果XYZ活動在焦點上,那麼我需要關閉按鈕上的活動。 3.如果XYZ活動未處於焦點狀態(如onPause)狀態,則需要更改按鈕狀態。通過intents啓動活動

你可以請幫我在我需要用來啓動意圖的標誌。 在我開始該活動之前是否可以獲得活動狀態?

回答

0

嘗試此

意圖意圖=新意圖(currentActivity.this,callingActivity.class); startActivity(intent);

您可以使用意圖這樣調用一個活動

0

按下該按鈕,將需要通過單獨每一項活動被捕獲,所以只是不同響應的代碼到每個不同的活動。

0

首先創建一個MAIN.java活動,以便容納您的其他活動。就像其他人所說的,你必須編寫自己的代碼才能捕捉自己,因爲如果你試圖處理意圖,這應該是常識。當你一起雖然,你可以像這樣通過意圖啓動一個新的活動:

// allocate new intent, initialized to the activity you wish to launch 
Intent i = new Intent(this, ActivityToBeLaunched.class); 

// put information into intent 
i.putExtras("KeyName", value); // where "KeyName" is simply a reference string 
         // and 'value' can be anything from boolean - string. 

// launch activity and wait for response 
startActivityForResult(i, REQUEST_CODE); 

然後你ActivityToBeLaunched.java類中,你將有一個OnCreate中,將來自像這樣的意圖拉動信息:

// get intent 
Intent i = this.getIntent(); 

// get information from intent 
booleanVariable = i.getExtras().getBoolean("KeyName"); 

當你完成這個活動,只需使用;

// create intent 
    Intent i = new Intent(); 

    // put information into result to send back to parent 
    i.putExtras("KeyName", value); 

    // set the result to be returned 
    setResult(i, ResultCode); 

    // finish child, return to parent with results 
    finish();