6
votes

I am developing an android app using Ionic Framework.

In that, I have three views, view1, view2 and view3.

Navigation will be like view1 -> view2 -> view3

When moving back to view2 from view3, I need to change a stateParam param1 value of view2 to perform some operations. Below code shows you that I am changing the stateParam value of view2 when clicking back button in view3

Following is the code to override the action of android device backbutton

$ionicPlatform.registerBackButtonAction(function() {

      if ($state.current.name == "view3") {
          $ionicHistory.backView().stateParams = {param1:true};
          $ionicHistory.goBack(); // moves to view2
      } else {
          $ionicHistory.goBack();
      }

}, 100);

After moving back to view2, If I click back button now, it is moving to view3 and not to view1. Now I am able to view only view3 and view2 by clicking back button, Not at all moving to view1.

How to make back button to move to view1 from view2 Even after changing the stateParam value of view2?

2
Try $state.go('view2',{param1:true});Emre
Emre, I tried it. But no luck. same results.Selva
It will be kind of hardcoded but can you create a switch-case and write code for every caseEmre

2 Answers

2
votes

I've finally found a solution to that issue, unfortuately, it's a hack so it's a little bit ugly.

The tip is to not only update stateParams, but also stateId.

Please see bellow a re-usable answer :

var params = {param1:true};
var backView = $ionicHistory.backView();

var stateId = backView.stateName;
for (var key in params) {
    if (params[key] && params[key] !== "") {
        stateId += "_" + key + "=" + params[key];
    }
}

backView.stateParams = params;
backView.stateId = stateId;
$ionicHistory.goBack();

In your specific case, it could just be:

if ($state.current.name == "view3") {
    var backView = $ionicHistory.backView();
    backView.stateParams = {param1:true};
    backView.stateId = backView.stateName + "_param1=true";
    $ionicHistory.goBack(); // moves to view2
} else {
    $ionicHistory.goBack();
}
1
votes

I met the same issue,but my view2 is root,so I disabled the back when view3 back to view2,thus made back view3 disappear

   $ionicHistory.backView().stateParams = resultObject;
   $ionicHistory.nextViewOptions({   disableBack: true });//this can
   $ionicHistory.goBack();

with this my code runs well,and for your destination,you can add a backview in view2.onenter() manually