후속 작업으로 인해 my previous question과 매우 유사합니다. 주어진 유일한 해결책에 나는별로 행복하지 않았습니다. 또한 해결책은이 문제와 약간 다른 문제에 대한 것이 었습니다. 그래서 다시 문제를 설명하려고합니다 ...알림은 활동을 열거 나 뒤로 단추를 눌러 뒤로 스택 활동을 막지 않습니까?
- 알림은 부팅시에 생성됩니다 (
BroadcastReceiver
). - 내 앱 주요 활동이 열리고 홈 버튼이 눌려집니다 (활동이 다시 스택으로 전송됩니다).
- 상태 표시 줄을 당기고 이전에 부팅시 생성 된 알림을 누릅니다.
- 주 활동과는 다른 활동이 시작됩니다.
- 뒤로 버튼을 누르면 주 활동이 표시됩니다.
이것은 이전 질문과 크게 다르지 않습니다. 중요한 것은 "주요 활동"이 그 예입니다. 앱 기본 활동을 연 다음 메뉴 옵션을 통해 about 활동을 연 다음 홈 버튼을 누를 수있었습니다. 백 스택은 이제 MainActivity
»AboutActivity
이됩니다. 다시 말해, "some activity"(알림을 눌러서 시작됨) 중에 "뒤로"버튼을 누를 때, 우리는 back 스택의 최상위, 즉 about 활동으로 넘어 가게됩니다.
기본적으로 원하는 것은 "일부 활동"(다시 말해서 알림을 눌러서 시작됨) 중에 "뒤로"버튼을 누르면 다른 활동이 열리지 않도록하는 것입니다. 내가 어디에 있었는지 정확하게 가져오고 싶다. 데스크톱이나 다른 앱의 활동 일 수도 있지만, 내 앱의 MainActivity
도 아니고 AboutAcitivity
도 아니고, 내가 있던 곳이 아닌 곳의 뒷쪽 스택에있는 "sleeping"배경에있다.
나는 해결책을 생각해 냈지만, 나는 그것이 매우 우아하다고 생각하지 않는다. 나는 더 잘, 더 우아하고, 뭔가를 찾고 있었다. 다른 제안이 있으면, 알려주세요. 어쨌든
이 내 제안 솔루션입니다 :// I use this class for public static (or public static final) members and
// methods
public final class AppHelper {
public static final String KEY_RESUME_FROM_NOTIFICATION = "resumeFromNotification";
private static boolean sResumeFromNotification = false;
public static boolean getResumeFromNotification() {
return sResumeFromNotification;
}
public static void setResumeFromNotification(boolean resumeFromNotification) {
sResumeFromNotification = resumeFromNotification;
}
}
public class MainActivity extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
(...)
}
@Override
protected void onResume() {
super.onResume();
if(AppHelper.getResumeFromNotification()) {
AppHelper.setResumeFromNotification(false);
moveTaskToBack(true);
}
}
}
public class AboutActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
(...)
}
@Override
protected void onResume() {
super.onResume();
if(AppHelper.getResumeFromNotification()) {
AppHelper.setResumeFromNotification(false);
moveTaskToBack(true);
}
}
}
public class SomeActivity extends Activity {
// This will be called when the notification is pressed and the activity is
// not opened yet
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
(...)
extractIntentExtras(intent);
}
// This will be called if the activity is already opened and the
// notification is pressed
@Override
protected void onNewIntent(Intent intent) {
extractIntentExtras(intent);
super.onNewIntent(intent);
}
private void extractIntentExtras(Intent intent) {
Bundle bundleExtras = intent.getExtras();
if(bundleExtras != null) {
// These intent extras are set on the Intent that starts this activity
// when the notification is pressed
AppHelper.setResumeFromNotification(bundleExtras.getBoolean(
AppHelper.KEY_RESUME_FROM_NOTIFICATION));
mRowId = bundleExtras.getLong(AgendaNotesAdapter.KEY_ROW_ID);
populateNoteUpdateFields();
}
}
}
나도 몰라,하지만이 솔루션은 나에게 매우 우아 보이지 않는 (하지만 난 그것을 기대대로 작동) 내가 찾고 있어요 제안 된 해결책에 대한 대안이나 강한 의견을 받아 들일 수있는 좋은 해결책으로 제시하십시오. 생각?
활동에 특별한 플래그가 있습니까? singleTop/singleInstance 또는 이와 비슷한 것? 알림은 새로운 작업 스택에서 활동을 시작해야하기 때문에 언급 한 활동을 보지 못했습니다 (FLAG_ACTIVITY_NEW_TASK 플래그가 필요함). –
나는 MainActivity (매니페스트에서)에'singleTop'을 가지고 있습니다. 나는 항상 'FLAG_ACTIVITY_NEW_TASK' 플래그로 혼란 스럽다. 알림에 의해 시작된 활동은 플래그가 설정되었는지 여부에 관계없이 작동합니다. 사용해야합니까, 사용하지 않아야합니까? –
내가 이해하는 바에 따르면 알림의 활동에 대해 항상 설정해야합니다. 상황에 따라 알림 작업을 일반 작업의 스택에 포함시키지 않으므로이 작업을 설정하는 것이 좋습니다. –