2012-05-22 2 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 프레임 워크는 사전 정의 된 활동 라이프 사이클을 따라 활동을 파괴하고 다시 만듭니다. 활동이 사용자에게 보이지 않을 때, 활동이 파괴되었을 가능성이 높으며 분명히 '일시 중지'되었습니다. 그러나 여러 인스턴스에서 유지하려는 액티비티의 인스턴스에 대해 무언가가 있다면 메서드 오버라이드를 통해 액티비티의 onCreate으로 전달 된 번들에 해당 인조 물을 배치 할 수 있습니다. 이렇게하면 활동이 떠난 것과 동일한 상태로 다시 생성 할 수 있습니다. 다시 작성하는 동안 사용자 입력을 잊지 않고 부분적으로 채워진 양식을 복원 할 수 있습니다. '백 스택'은 내가 100 %가 아닌 다른 메커니즘을 사용할 수 있습니다.

+0

도움 주세페? 내 대답이 충분하지 않은 경우 더 자세하게 게시 할 수 있습니다. – OceanLife

+0

방금 ​​너무 늦게 대답을 읽었습니다. 고마워요. – Giuseppe

관련 문제