2012-05-22 39 views
1

我正在用以下代碼創建一個主頁快捷方式: 我想獲得啓動的應用程序的相同打開實例(如果存在),而不是新實例。Android:啓動相同的實例

public void createShortcut() { 

    if (app.prefs.getBoolean("prefs_ShortcutCreated", false) == false) { 
     Intent shortcutIntent = new Intent(); 
     shortcutIntent.setClassName(this, this.getClass().getName()); 

     shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); 

     Intent addIntent = new Intent(); 
     addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent); 
     addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "test"); 
     addIntent.putExtra("duplicate", false); 
     File image = new File(app.DEFAULT_APP_FOLDER_MAIN, "icon.png"); 
     AssetManager assets = app.getApplicationContext().getResources().getAssets(); 
     try { 
      copy(assets.open("icon.png"), image); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     Bitmap theBitmap = BitmapFactory.decodeFile(app.DEFAULT_APP_FOLDER_MAIN + "icon.png"); 
     Bitmap scaledBitmap = Bitmap.createScaledBitmap(theBitmap, 128, 128, true); 
     addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON, scaledBitmap); 

     addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT"); 
     sendBroadcast(addIntent); 

     theBitmap.recycle(); 
     scaledBitmap.recycle(); 

     Editor editor = app.prefs.edit(); 
     editor.putBoolean("prefs_ShortcutCreated", true); 
     editor.commit(); 
    } 

} 

回答

1

Android框架破壞並重新創建以下預定義活動的生命週期的活動。當一個活動對用戶不可見時,它很可能已經被破壞,並且已經被「暫停」了。但是,如果您希望跨實例維護某個活動的實例,則可以將這些工件放入通過onSaveInstanceState方法覆蓋交付到活動的onCreate的Bundle中。這允許活動以與它離開時相同的狀態被重新創建,例如,恢復部分填寫的表單,而不是在重新創建期間忘記用戶輸入。 '後退'可能會採用我不是100%的不同機制。

+0

任何幫助朱塞佩?如果我的答案不夠,請隨時發佈更多詳細信息。 – OceanLife

+0

我剛剛閱讀你的答案太晚了,謝謝。 – Giuseppe

相關問題