Comments (11)
My fault, I accidently called the presenter to update what it's showing in onCreate, since I need to pass it the current object that needs to be displayed from the intent the activity received. Is there any good way to do this? there is nothing in the presenter code that receives intent from onCreate and from onNewIntent, but this is certainly needed to be dealt with by the presenter. I am using RxJava, should I use a BehaviourSubject?
Thanks.
from thirtyinch.
Not exactly had this problem with onNewIntent()
but with onActivityResult
. The onActivityResult
case is not really solved as of now. The problem is that onActivityResult
is called before onCreate
. Saving the result in a variable and request it from the view in onWakeUp()
is the current "solution".
The onNewIntent()
case can be solved with the same "solution". A easy and predefined way to work with such events should be part of a future release.
from thirtyinch.
I think I didn't explain myself enough. I am implementing a BrowserActivity with the android WebView. In onCreate I receive an url from intent.getData() and I need to pass it to the SitePresenter to instruct the view to display the site (if allowed by business logic). If a new intent is sent while the activity is open, it should update the current site being displayed by calling onSiteLoadRequest() in the presenter. So, who is responsible in saving the state in onSaveInstanceState and restoring it in onCreate?
Also, I want the WebView to restore itself to the current page when the screen orientation changes, but when I save it's state in onSaveInstanceState and load it in onCreate, my onWakeUp gets called and loads the home page of that site after the web view restores it's own state.
I'm a bit confused what to do in such a situation?
from thirtyinch.
ok, simple. Don't use onSaveInstanceState, save the state in the presenter. when you receive a new intent pass it immediately to the presenter (I hope this happens after Activity#onCreate
), maintain the state there and when the view attaches update it accordingly
from thirtyinch.
My problem is that the WebView gives a bundle of its current state, and restores from that bundle. Should I be saving the bundle in the presenter in onDetachView and restoring it at onAttachView, or is this making the presenter too attached to Android api and complicates testing.
from thirtyinch.
Oh, interesting. Hmm...
You cannot use Bundle
in your tests, therefore it's too close to the Android SDK. Sadly I don't have a good solution for you here. WebView Activities often are very simple and don't have business logic. Is MVP here really helpful?
A WebView is so close to the Android SDK, I don't think a MVP abstraction is very useful here.
from thirtyinch.
There's plenty of logic, since this WebView needs to be filtered. Certain links are permitted to go to while others not.
Maybe something along the idea of Icepick would solve this problem, but it would add an additional layer of transformation between the view and the presenter... But the idea would be that the Presenter base class would handle all the View restoration bundles behind the scenes, gaining the presenter's lifecycle instead of the view lifecycle and restoration by parcelers.
When needed, the activity could pass view's bundles (like the webview's) to the presenter in the activity code, not in the presenter's implementation (or possibly, the saveInstance and onCreate could automatically assign them to the Presenter in TiActivity code...). What do you think?
from thirtyinch.
I thought about this a lot. I came to the conclusion that saving the state should be done in the view (Activity
) in onSaveInstance state. It's so tied to the Activity lifecycle and shouldn't be moved to the presenter. The presenter is still able to provide data (such as URIs) and filter them but you should not put any parcelable to the presenter.
See it from this side: Clean MVP would send a link to the webview whenever it get's attached. The webview always loads the given page. This can be very fast especially for local HTML files.
But you don't want to reload the page every time because it's slow. So you cache data in the view layer (Bundle).
Another thing: The WebView
stores a lot of state in the View layer such as the previous page stack, images, websites,... everything a browser knows. This is problematic when you try to save the state in the presenter. The WebView simply doesn't work that way.
from thirtyinch.
What about having a isRestoredFromSavedState() in the Presenter<> class (just like there is isViewAttached()..), since certain presenter logic depends on this... Is this good practice? If so, could this possibly come out in the next version? Thanks!
from thirtyinch.
The presenter lifecycle will not change in the near future. 4 call backs, create/destroy, view attached/detached that's it. When something requires Activity lifecycle methods it should life in the view.
from thirtyinch.
WebView and MVP doen't work together very well, at least not when the presenter is retaining.
from thirtyinch.
Related Issues (20)
- Debugging a @CallOnMainThread annotated method crashes the app HOT 2
- Create Kotlin module/extensions HOT 2
- Switch from compileOnly to implementation HOT 1
- [Discussion] Should manageViewDisposable() throw an exception
- Support for AndroidX HOT 4
- BackstackReader doesn't work with AndroidX HOT 3
- "Don't add observers when the presenter reached the DESTROYED state" EXCEPTION HOT 1
- sendToView HOT 4
- Lint warning with sendToView, but not with deliverToView
- Why saving the Presenter on orientation change HOT 1
- Problems with @CallOnMainThread
- Lint does not report by abstract classes without implementation View
- Crash using AndroidX with ProGuard enabled HOT 2
- Lint reports: Obsolete custom lint check
- bundle[buildType] was replaced with AGP 3.2.0
- Package thirtyinch-kotlin-coroutines not published HOT 3
- Plugin module not available with version 0.9.5 HOT 2
- Release for androidx HOT 2
- Restore old BackstackReader.isInBackstack() implementation once AndroidX Fragment 1.2.1 is released HOT 1
- Migrate away from JCenter HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from thirtyinch.