Is there any way to re-initialize $viewHistory data?


#1

I’m doing an hybrid iOS app which use ionicframework as the frontend framework. Sometimes in my app, I need to close the UIviewweb and free memory from UIwebview before doing some heavy procesing (it could be 3d rendering, etc). After that, I want to re-open the webview with the same state as it was.

I approached this problem by saving $rootScope.$viewHistory data before closing the webview. However, I don’t know how to restore it when I re-open the webview.

Is there anyone know how to do it?

Thank you very much for reading & helping.

P


#2

Hi @phamaa! Good question. It sounds like you have a good start.

The way I would do it is (sounds like you already did first 3 steps):

  1. localStorage.setItem('ionicHistory', JSON.stringify($rootScope.viewHistory)

  2. var history = JSON.parse(localStorage.getItem('ionicHistory'));

  3. $rootScope.$viewHistory = history;

  4. $rootScope.$broadcast('viewState.changeHistory', { historyId: history.currentView.historyId });


#3

I don’t suggest to use localStorage unless you wrap it in a try/catch as it will break iOS if the user is in “private browsing” mode or has cookies disabled (yes… I did say cookies).


#4

Thanks Andy for the clear instruction. It works well in my case.

I agree with Jason in avoiding using localStorage because it isn’t so “stable”.

Instead of using localStorage, I wrote the history into a file called historyState.json by using native iOS file system. Before closing the webview, I will override it.And everytime I re-open the webview, I just make a request to historyState.json and append its content to $rootScope.$viewHistory


#5

@andytjoslin

I tried restoring the viewHistory again today with ionic 0.9.25.

When i run this broadcast function

$rootScope.$broadcast('viewState.changeHistory', { historyId: history.currentView.historyId });

I got this error

@andytjoslin , do you have any ideas about it?

Thanks!


#6

I’ve realised that JSON.stringify($rootScope.viewHistory) skipped all functions and method of $rootScope.viewHistory. That might be the reason I couldn’t reinitialise $rootScope.viewHistory

Is there anybody know a proper way to store and reinitialise $rootScope.viewHistory with ionic?

Thank you


#7

This is the only way I know how : $ionicViewService.clearHistory()


#8

I think $ionicViewService.clearHistory() is only for clearing histories. I want to reinitialise $rootScope.$viewHistory from a hierarchy json file such as

{“histories”:{“root”:{“historyId”:“root”,“parentHistoryId”:null,“stack”:[{“viewId”:“002”,“index”:0,“historyId”:“root”,“backViewId”:null,“forwardViewId”:“004”,“stateId”:“app.home_mode=local”,“stateName”:“app.home”,“stateParams”:{“mode”:“local”},“url”:"/local/home"},{“viewId”:“004”,“index”:1,“historyId”:“root”,“backViewId”:“002”,“forwardViewId”:“005”,“stateId”:“app.menu_mode=local_menuId=root”,“stateName”:“app.menu”,“stateParams”:{“mode”:“local”,“menuId”:“root”,“productID”:null},“url”:"/local/menu/root"},{“viewId”:“005”,“index”:2,“historyId”:“root”,“backViewId”:“004”,“forwardViewId”:null,“stateId”:“app.menu_mode=local_menuId=L1Alessi”,“stateName”:“app.menu”,“stateParams”:{“mode”:“local”,“menuId”:“L1Alessi”,“productID”:null},“url”:"/local/menu/L1Alessi"}],“cursor”:2}},“views”:{“002”:{“viewId”:“002”,“index”:0,“historyId”:“root”,“backViewId”:null,“forwardViewId”:“004”,“stateId”:“app.home_mode=local”,“stateName”:“app.home”,“stateParams”:{“mode”:“local”},“url”:"/local/home"},“003”:{“viewId”:“003”,“index”:1,“historyId”:“root”,“backViewId”:“002”,“forwardViewId”:null,“stateId”:“app.menu_mode=local_menuId=notroot,L1Alessi”,“stateName”:“app.menu”,“stateParams”:{“mode”:“local”,“menuId”:“notroot,L1Alessi”,“productID”:null},“url”:"/local/menu/notroot,L1Alessi"},“004”:{“viewId”:“004”,“index”:1,“historyId”:“root”,“backViewId”:“002”,“forwardViewId”:“005”,“stateId”:“app.menu_mode=local_menuId=root”,“stateName”:“app.menu”,“stateParams”:{“mode”:“local”,“menuId”:“root”,“productID”:null},“url”:"/local/menu/root"},“005”:{“viewId”:“005”,“index”:2,“historyId”:“root”,“backViewId”:“004”,“forwardViewId”:null,“stateId”:“app.menu_mode=local_menuId=L1Alessi”,“stateName”:“app.menu”,“stateParams”:{“mode”:“local”,“menuId”:“L1Alessi”,“productID”:null},“url”:"/local/menu/L1Alessi"}},“backView”:{“viewId”:“004”,“index”:1,“historyId”:“root”,“backViewId”:“002”,“forwardViewId”:“005”,“stateId”:“app.menu_mode=local_menuId=root”,“stateName”:“app.menu”,“stateParams”:{“mode”:“local”,“menuId”:“root”,“productID”:null},“url”:"/local/menu/root"},“forwardView”:null,“currentView”:{“viewId”:“005”,“index”:2,“historyId”:“root”,“backViewId”:“004”,“forwardViewId”:null,“stateId”:“app.menu_mode=local_menuId=L1Alessi”,“stateName”:“app.menu”,“stateParams”:{“mode”:“local”,“menuId”:“L1Alessi”,“productID”:null},“url”:"/local/menu/L1Alessi"},“disabledRegistrableTagNames”:[“ION-TABS”,“ION-SIDE-MENUS”]}