4

Using startActivityForResult doesn't work properly in Android < 5.0 when using launchMode singleInstance or singleTask. However, I'm using that launchMode to avoid WebView reload:

android:alwaysRetainTaskState="true"
android:documentLaunchMode="never"
android:launchMode="singleInstance"

I understand there's no possible workaround around the first limitation.

Is there a way to make those Manifest attributes variables, depending on the API version?
(I'd use those three attributes with specified values for Android > 5.0, and a launchMode="standard" for Android < 5.0. Very similar to this, only that I can't find a way to define documentLaunchMode and launchMode; only alwaysRetainTaskState since it's a bool).

Community
  • 1
  • 1
RominaV
  • 3,335
  • 1
  • 29
  • 59
  • I don't understand why you need to use a special launch mode to avoid `WebView` reload. Can you elaborate? – David Wasser Dec 09 '16 at 10:14
  • Hi @DavidWasser , well I'm not sure if it has to do with how the website was done, but each time the apps goes to background, when I resume it, the WebView reloads, so any changes that were done to the HTML are lost. I googled and found that the answer [here](http://stackoverflow.com/questions/28096779/how-to-save-webview-state-and-restore-it-in-android-lollipop) worked in my case. – RominaV Dec 09 '16 at 15:44
  • Sounds very strange. In general, special launch modes `singleInstance` and `singleTask` should not be used. They usually create more problems than they solve. But maybe that's a topic for another day ;-) – David Wasser Dec 09 '16 at 17:36

1 Answers1

2

You can create an <activity-alias> that uses the same underlying Activity, but has different manifest parameters (launch mode, etc.). Then you just need to make sure that you launch the correct Activity (either the original one, or the alias) depending on what version of Android you are running on.

Depending on your architecture and the nature of your problem, this may or may not be an appropriate solution.

David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • 1
    What a neat idea, I didn't know about . I'll try this out. – RominaV Dec 09 '16 at 15:37
  • Well, it didn't work for my specific case (using singleInstance and startActivityForResult in Android < 5) but I think this is a possible solution when someone is looking to have Activity attributes depending on a condition. Of course, I'm still trying to sort out why the WebView behaves like this but that's another issue :P – RominaV Dec 11 '16 at 18:15
  • OK. Good luck with that! – David Wasser Dec 11 '16 at 19:25