2014-11-21 79 views
0

我有兩個應用程序,A和B在我加入這將打開應用程序B. 當按鈕被點擊我想,如果打開應用B按鈕的應用程序一它還沒有運行,否則我想把應用程序放在前面。應用按鈕打開另一個應用程序的當前/新實例

這是我使用的代碼:

Intent intent = getPackageManager() 
      .getLaunchIntentForPackage(
      "com.myapp.something"); 
if (intent != null) { 
    intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); 
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    startActivity(intent); 
} 

的代碼似乎工作,但問題是,如果我從應用程序A打開應用程序B(使用此代碼),然後直接單擊應用程序的圖標B,我得到了兩個應用程序實例,這是不受歡迎的。

我怎麼能打開應用程序,因爲代碼的功能,而且還可以獲得即使單擊該應用程序的圖標相同的實例?

回答

0

添加Intent.FLAG_ACTIVITY_CLEAR_TOP

+0

TX,但如果我從它的圖標來打開應用B我仍然得到應用的新實例。 – 2014-11-21 14:32:02

+0

這是應用B你自己?如果沒有,我不認爲你能做更多的事情。如果是的話,你可以在清單的應用程序B. – Carnal 2014-11-21 14:52:20

+0

添加launchMode是singleInstance這兩個應用程序是我的。我會試試這個。 – 2014-11-21 14:53:18

0

嘗試這種::

Intent intent = context.getPackageManager().getLaunchIntentForPackage(yr packageName); if (intent != null) { /* We found the activity now start the activity */ intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); } else { /* Bring user to the market or let them choose an app? */ intent = new Intent(Intent.ACTION_VIEW); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setData(Uri.parse("market://details?id=" + packageName)); startActivity(intent); }

+0

我已經嘗試過此代碼,問題是,它會打開應用程序的新實例,即使該應用程序已經在運行,而不是把它帶到前面。 – 2014-11-21 14:44:47

相關問題