2011-12-28 4 views
0

수정 :주의 사항 : 저는 완전히 내 질문을 다시 설명했습니다. B :Android : 알림을 클릭하면 항상 최고의 활동을 시작합니다.

나는 두 Activites와 응용 프로그램이 있습니다. ActivityMAIN이다. 그러면 응용 프로그램이 시작되고 A이 화면에 나타납니다. 사용자가 버튼을 누르면 새로운 ActivityB이 화면에 나타납니다.

따라서 "백 스택"에는 두 가지 활동이 있습니다 : 입니다.

는 지금은 "홈"키를 누른 다음 실행에 내 응용 프로그램의 아이콘을 클릭 : Activity B는 지금까지 내 작업에서 최고 활동이기 때문에, 화면 ( 아니)에 나타납니다.

지금 질문 : 내가 Intent을 열어도 현재 내 업무의 상단 Activity을 열 수 있습니까?

Notification에서 사용해야합니다. 사용자가 내 Notification을 클릭하면이 작업의 상단 Activity이 지정된 것이 아니라 화면에 나타나야합니다.

SINGLE_TOP과 같은 인 텐트 플래그를 여러 번 시도했지만 여전히 필요한 것을 얻을 수 없습니다.

해결책을 아는 사람이 있습니까?

답변

7

글쎄, 몇 일 몇 시간을 보낸 이후, 나는 해결책을 발견 :

Intent notificationIntent = new Intent(this, MainActivity.class); 
notificationIntent.setAction(Intent.ACTION_MAIN); 
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER); 
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); 

실행 응용 프로그램을 시작하는 방법, 그리고 너무 알림 의도을 위해 좋은 작동합니다.

+0

매력처럼 작동합니다! – Defuera

0

android : launchMode = "singleInstance"를 사용할 수 있습니다. 그럴 수 있습니다.

+0

아니요, 물론 시도했지만 제대로 작동하지 않습니다. (내 시나리오를보세요) –

1

이렇게 사용했습니다.

private void notifyUser(Context context) { 

    notificationManager = (NotificationManager) context 
      .getSystemService(Context.NOTIFICATION_SERVICE); 
    CharSequence text = "Test Notification"; 
    Notification notification = new Notification(R.drawable.icon, text, 
      System.currentTimeMillis()); 

    Intent resultIntent = new Intent(context, StartActivity.class); 
    resultIntent.setAction(Intent.ACTION_MAIN); 
    resultIntent.addCategory(Intent.CATEGORY_LAUNCHER); 
    resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK 
      | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); 

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context); 
    // Adds the back stack for the Intent (but not the Intent itself) 
    stackBuilder.addParentStack(GeneralSettingsActivity.class); 
    // Adds the Intent that starts the Activity to the top of the stack 
    stackBuilder.addNextIntent(resultIntent); 
    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, 
      PendingIntent.FLAG_UPDATE_CURRENT); 

    notification.flags = Notification.FLAG_AUTO_CANCEL; 
    notification.setLatestEventInfo(context, "Title", text, 
      resultPendingIntent); 
    notificationManager.notify(AUTOSYNC_COMPLETED_NOTIFICATION, 
      notification); 

} 
관련 문제