2014-02-05 45 views
1

我有一個應用程序首先啓動不是MainActivity的活動,但它可以在應用程序的過程中自行啓動活動。我希望在活動關閉時運行的代碼能夠確定它是否應該放在應用程序的前端(第一次運行),或者是否應該返回到堆棧上的前一個活動(所有其他運行)。是否有可能確定一個活動是如何在它內部開始的?確定如何開始活動

+0

究竟你是「確定的活動是如何在其內部啓動了」是什麼意思?請更清楚一點或舉一個更具體的例子。 –

+0

David-我想在兒童活動的過程中確定什麼家長活動開始了孩子。在我的情況下,無論是啓動器還是MainActivity。 – UprightCitizen

回答

1

你說:

我想確定孩子的過程中 活動父活動開始子活動。在我的情況下,將 或者是啓動器或MainActivity。

不幸的是,沒有辦法找出哪些Activity啓動了您的活動。這個信息是不可用的。然而...

您可以告訴我們,如果發射器通過檢查IntentACTION = MAINCATEGORY = LAUNCHER開始你的活動:

Intent intent = getIntent(); 
if (Intent.ACTION_MAIN.equals(intent.getAction()) && intent.hasCategory(Intent.CATEGORY_LAUNCHER)) { 
    // started by launcher 
} 

您還可以檢查活動是從最近的任務列表中通過檢查啓動爲Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY這樣的:

Intent intent = getIntent(); 
if ((intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0) { 
    // Launched from recent task list 
} 

如果這還不夠你,那麼你可以隨時啓動從父孩子活動時,增加一個「額外」的自己,好讓這不可能活動開始了什麼?例如:

Intent intent = new Intent(this, ChildActivity.class); 
intent.putExtra("startedFromMainActivity", true); 
startActivity(intent); 

,然後在你的孩子的活動,您可以檢查這樣的:

Intent intent = getIntent(); 
if (intent.hasExtra("startedFromMainActivity") { 
    // started from MainActivity 
} 
0

可以存儲在意圖的值啓動您的活動,而一旦打開閱讀,以適應您的行爲:

intent.putExtra(key,value); 

而且在活動側(在的onCreate爲EG):

getIntent().getExtra(key,defaultValue); 

如果未找到任何值,則會得到默認值。 getExtra取決於Ø存儲的數據的類型,所以getIntExtra,booleanExtra,stringExtra ...
瞭解更多here

0

你的宣言文件交換mainactivity DEFAULT ...

 <activity 
     android:name="com.example.iiintent.MainActivity" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.DEFAULT" /> 
     </intent-filter> 
    </activity> 
    <activity android:name="com.example.iiintent.al"> 
    <intent-filter> 


      <category android:name="android.intent.category.LAUNCHER" /> 
    </activity> 
+0

這沒有幫助。如果您像這樣更改清單,那麼您的應用程序將不會在可用應用程序列表中結束。您需要爲「ACTION = MAIN」和「CATEGORY = LAUNCHER」製作一個活動,以便您的應用在可用應用列表中顯示。 –

+0

非常感謝你... – fsm