2017-10-26 2 views
1

활동이 A, B입니다. A는 게시물 목록이고 B는 게시물을 보여줍니다. 그래서 적재 할 수있는 다중를 시작할 때 나는개시 이후 연기가 시작되지 않음

PendingIntent.getActivity(context, (int) id, intentForB, PendingIntent.FLAG_UPDATE_CURRENT); 

B가 전혀 lunchmode없이 의도 플래그가없는 것처럼 활동 B를 시작 알림을 만들 수 있습니다.

이것은 알림을 클릭했을 때 예상 한 것입니다.

A -> (열기를 NotI) -> B

A -> B -> (오픈을 NotI) - 지금까지> 또 다른 B

, 모든 게 잘. 하지만 위의 의도에서 app을 시작하면 문제가 발생합니다.

응용 프로그램이 백그라운드에 존재하지 않을 때 알림을 열면 B 독립 실행 형을 표시합니다. 그리고 다른 알림을 열면 아무것도 표시되지 않습니다.

(open noti) -> B -> (open not another noti) -> 다른 B가 예상되지만 아무 일도 없었습니다.

FLAG_ACTIVITY_NEW_TASK를 B에 놓으면 B가 표시되지만 이전의 것은 파괴됩니다. 이것은 내가 원하는 것이 아닙니다.

내 질문은, 이유는 new_task 또는 clear_top 플래그가없는 응용 프로그램을 보류중인 의도에서 열 때 작동하지 않습니다? 어떻게 작동시킬 수 있습니까?

+0

알림을 열었을 때 응용 프로그램이 전혀 실행되지 않으면 어떻게 될까요? –

답변

0

원하는 동작을 얻으려면 B를 좀 더 똑똑하게 만들어야 할 것입니다. 앱이 실행되고 있지 않을 때 Notification에서 시작된 경우에 해당되는 작업 ("Activity")에 "루트"Activity으로 실행 된시기를 알아야합니다.

"루트"Activity으로 시작한 경우 작업을 지우고 A를 실행하고 B에게 자동으로 B를 시작해야한다고 알려서 A로 리디렉션해야합니다. 이렇게하려면 B.onCreate() 이 작업을 수행 :

super.onCreate(...); 
if (isTaskRoot()) { 
    // We need to redirect the user to A by restarting the task 
    Intent redirectIntent = new Intent(this, A.class); 
    redirectIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); 
    // Add extras so that A will automatically launch B 
    redirectIntent.putExtra("launchB", true); 
    // add any other extras from the original intent 
    redirectIntent.putExtras(getIntent()); 
    startActivity(redirectIntent); 
    // We are done, don't do anything else 
    finish(); 
    return; 
} 

를 이제, 당신이 자동으로 A.onCreate()이 코드를 추가 B.는이를 위해 실행해야합니다

super.onCreate(...); 
... rest of code from A.onCreate()... 
if (getIntent().hasExtra("launchB")) { 
    // We should immediately launch B because the user launched B from 
    // from a Notification when the app was not running 
    Intent launchIntent = new Intent(this, B.class); 
    // Add any extras from the original Intent 
    launchIntent.putExtras(getIntent()); 
    startActivity(launchIntent); 
} 

이 항상 보장 할 것이라는 당신의 작업의 루트입니다 .