Quando si prova a startActivityForResult
per Activity
che dispone di launchMode="singleTask"
; non restituirà alcun valore con onActivityResult
e quando si imposta launchMode="standard"
; tutto funziona bene, ma i requisiti di sistema dicono che questo Activity
deve essere singleTask
, c'è comunque da risolvere?onActivityResult con launchMode = "singleTask"?
risposta
La documentazione del startActivityForResult
dicono:
For example, if the activity you are launching uses the singleTask launch mode,
it will not run in your task and thus you will immediately receive a cancel result.
Sembra che non v'è alcun modo per aggirare questo.
Se sei lo sviluppatore di Attività chiamata, puoi farlo inviare una trasmissione quando sono disponibili alcuni risultati. L'attività chiamante può quindi elencare queste trasmissioni.
Cosa dice @Peter Knego
più
che sembra funzionare a 5.1, non in 4.4.4
senso che onActivityResult incendi
Sì, ho anche osservato che funziona in 5.o e sopra non in 4.4.4 – Mahesh
La risposta mostra nella funzione di startActivityUncheckedLocked
classe ActivityStackSupervisor
. Prima di Android 5.x, all'avvio di un'attività, controlla prima launchMode e aggiunge FLAG_ACTIVITY_NEW_TASK
all'avvio di Flags se launchMode è singleTask o singleInstance. Se launchFlags dell'attività contiene FLAG_ACTIVITY_NEW_TASK
, invierà immediatamente un annullamento e lascerà che la nuova attività continui come di consueto senza una dipendenza dal suo mittente.
if (sourceRecord == null) {
// This activity is not being started from another... in this
// case we -always- start a new task.
if ((launchFlags&Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
Slog.w(TAG, "startActivity called from non-Activity context; forcing " +
"Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
}
} else if (sourceRecord.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
// The original activity who is starting us is running as a single
// instance... this new activity it is starting must go on its
// own task.
launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
} else if (r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE
|| r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK) {
// The activity being started is a single instance... it always
// gets launched into its own task.
launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
}
// ......
if (r.resultTo != null && (launchFlags&Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
// For whatever reason this activity is being launched into a new
// task... yet the caller has requested a result back. Well, that
// is pretty messed up, so instead immediately send back a cancel
// and let the new task continue launched as normal without a
// dependency on its originator.
Slog.w(TAG, "Activity is launching as a new task, so cancelling activity result.");
r.resultTo.task.stack.sendActivityResultLocked(-1,
r.resultTo, r.resultWho, r.requestCode,
Activity.RESULT_CANCELED, null);
r.resultTo = null;
}
Ma in 5.x Android, questo è stato cambiato come di seguito:
final boolean launchSingleTop = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP;
final boolean launchSingleInstance = r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE;
final boolean launchSingleTask = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK;
int launchFlags = intent.getFlags();
if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 &&
(launchSingleInstance || launchSingleTask)) {
// We have a conflict between the Intent and the Activity manifest, manifest wins.
Slog.i(TAG, "Ignoring FLAG_ACTIVITY_NEW_DOCUMENT, launchMode is " +
"\"singleInstance\" or \"singleTask\"");
launchFlags &=
~(Intent.FLAG_ACTIVITY_NEW_DOCUMENT | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
} else {
switch (r.info.documentLaunchMode) {
case ActivityInfo.DOCUMENT_LAUNCH_NONE:
break;
case ActivityInfo.DOCUMENT_LAUNCH_INTO_EXISTING:
launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
break;
case ActivityInfo.DOCUMENT_LAUNCH_ALWAYS:
launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
break;
case ActivityInfo.DOCUMENT_LAUNCH_NEVER:
launchFlags &= ~Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
break;
}
}
final boolean launchTaskBehind = r.mLaunchTaskBehind
&& !launchSingleTask && !launchSingleInstance
&& (launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0;
if (r.resultTo != null && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
// For whatever reason this activity is being launched into a new
// task... yet the caller has requested a result back. Well, that
// is pretty messed up, so instead immediately send back a cancel
// and let the new task continue launched as normal without a
// dependency on its originator.
Slog.w(TAG, "Activity is launching as a new task, so cancelling activity result.");
r.resultTo.task.stack.sendActivityResultLocked(-1,
r.resultTo, r.resultWho, r.requestCode,
Activity.RESULT_CANCELED, null);
r.resultTo = null;
}
Ecco perché onActivityResult
opere in Android 5.x anche si imposta launchMode a singleTask
o singleInstance
.
So che questo è abbastanza tardi ma è possibile avere OnActivityResult tipo di effetto sul metodo onNewIntent() perché questa è l'attività singleTask.
- 1. Informazioni su Android launchmode "singleTask"
- 2. Impostazione launchMode = "singleTask" vs impostazione activity launchMode = "singleTop"
- 3. Android launchmode = "singleTask" non funziona come previsto
- 4. startActivityForResult non funziona correttamente con launchMode singleInstance
- 5. Android: riprendere un'attività con singleTop o SingleTask
- 6. Android onActivityResult chiamato in anticipo
- 7. Android singletop SingleInstance e singletask
- 8. Attività Launcher + singleTask in Android
- 9. onActivityResult ritorna con i dati = null
- 10. FLAG_ACTIVITY_CLEAR_TOP e onActivityResult
- 11. Utilizzare onactivityresult android
- 12. Nuovo intent() avvia una nuova istanza con Android: launchMode = "singleTop"
- 13. non onActivityResult essere chiamato
- 14. in onActivityResult sono nulli
- 15. Attività e processo Android, SingleTask e SingleInstance
- 16. Modalità di avvio Android singleTask o singleInstance?
- 17. OnActivityResult non chiamato dopo startIntentSenderForResult
- 18. onActivityResult() Errore di transazione frammento
- 19. Handle onActivityResult su un servizio
- 20. Android ritorno android.provider.MediaStore.ACTION_VIDEO_CAPTURE onActivityResult nulla con Nexus 7
- 21. ciclo vitale Android onActivityResult vs onStop
- 22. Android: onActivityResult in frammento non chiamato
- 23. onActivityResult() non viene eseguito in DialogFragment
- 24. come ottenere risultati da onActivityResult in Fragment?
- 25. È necessario utilizzare super.onActivityResult() in onActivityResult()?
- 26. : il risultatoCodice di onActivityResult è sempre 0
- 27. Il metodo onActivityResult non viene chiamato Android
- 28. Come rimuovere l'attività singleTask con taskAffinity dalle attività recenti al termine?
- 29. onActivityResult() non chiamato quando Attività avviata da Frammento
- 30. onActivityResult viene chiamato all'avvio dell'attività, non quando è finito
Quindi come impedire la creazione di più istanze di (ad esempio una lista) in onClick? – Imon
Imon, utilizzare singleTop – joox