B-Stewart
B-Stewart

Reputation: 697

Cordova AngularJS navigation on app return from external URL not remembering history

I'm working on a Cordova application using Ionic (with AngularJS) and I have hit a problem. My app has external links that open in a browser. When I tap on these links I am taken to the web page, however upon hitting the back button to return to the app, I have lost my back button and my home button does not work, effectively rendering my navigation useless. I'm not entirely sure how to handle this, as if I simply leave the app and re-enter it navigation works fine so it seems to be when linked to something from the app itself. It happens on Android and on my computer running the files in my web browser. Ideally getting navigation to remember where it was and provide accurate 'back' functionality would be great, but even just sending me back to the home page would be useful. I've looked a little into routing and locations, though I'm not quite sure how they fit into a Cordova App. Any guidance would be appreciated, as I am fairly new to angularJS.

app.js

var app = angular.module('app', ['ionic']);

app.config(function ($stateProvider, $urlRouterProvider) {

    $stateProvider
      .state('tabs', {
          url: "/tab",
          abstract: true,
          templateUrl: "templates/tabs.html"
      })
      .state('tabs.home', {
          url: "/home",
          views: {
              'home-tab': {
                  templateUrl: "templates/home.html",
                  controller: 'HomeTabCtrl'
              }
          }
      })
      .state('tabs.resources', {
          url: "/resources",
          views: {
              'home-tab': {
                  templateUrl: "templates/resources.html",
                  controller: "AdditionalResourcesCtrl"
              }
          }
      })
      .state('tabs.gmetrics', {
          url: "/gmetrics",
          views: {
              'home-tab': {
                  templateUrl: "templates/gmetrics.html",
                  controller: "GlobalMetricsCtrl"
              }
          }
      })
      .state('tabs.gfeed', {
          url: "/gfeed",
          views: {
              'home-tab': {
                  templateUrl: "templates/gfeed.html"
              }
          }
      })
      .state('tabs.googleforms', {
          url: "/googleforms",
          views: {
              'home-tab': {
                  templateUrl: "templates/googleforms.html"
              }
          }
      })
      .state('tabs.social', {
          url: "/social",
          views: {
              'home-tab': {
                  templateUrl: "templates/social.html",
                  controller: "SocialNetworkCtrl"
              }
          }
      })
    .state('tabs.chapters', {
        url: "/chapters",
        views: {
            'home-tab': {
                templateUrl: "templates/chapters.html"
            }
        }
    });

    $urlRouterProvider.otherwise("/tab/home");

});

app.config(['$ionicConfigProvider', function ($ionicConfigProvider) {

    $ionicConfigProvider.tabs.position('bottom'); //other values: top

}]);
/*
app.config( ['$compileProvider', function( $compileProvider ){   
    $compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|ftp|mailto):/);
}]);
*/

Partial index.html:

<body ng-app ="app">

    <ion-nav-bar class="bar-positive">
        <ion-nav-back-button>
        </ion-nav-back-button>
    </ion-nav-bar>

    <ion-nav-view></ion-nav-view>


    <script id="templates/tabs.html" type="text/ng-template">
        <ion-tabs class="tabs-icon-top tabs-positive">

            <ion-tab title="Home" icon="ion-home" href="#/tab/home">
                <ion-nav-view name="home-tab"></ion-nav-view>
            </ion-tab>
            <!--
            <ion-tab title="About" icon="ion-ios-information" href="#/tab/about">
                <ion-nav-view name="about-tab"></ion-nav-view>
            </ion-tab>

            <ion-tab title="Contact" icon="ion-ios-world" ui-sref="tabs.contact">
                <ion-nav-view name="contact-tab"></ion-nav-view>
            </ion-tab>
                -->
        </ion-tabs>
    </script>

    <script id="templates/home.html" type="text/ng-template">
        <ion-view view-title="Home">
            <ion-content class="padding">
                <div class="list">
                    <a class="item item-icon-right" href="#/tab/resources">
                        <i class="icon ion-chevron-right"></i>
                        Additional Resources
                    </a>
                    <a class="item item-icon-right" href="#/tab/gmetrics">
                        <i class="icon ion-chevron-right"></i>
                        Global Metrics
                    </a>
                    <a class="item item-icon-right" href="#/tab/gfeed">
                        <i class="icon ion-chevron-right"></i>
                        Global Feed
                    </a>
                    <a class="item item-icon-right" href="#/tab/googleforms">
                        <i class="icon ion-chevron-right"></i>
                        Google Forms
                    </a>
                    <a class="item item-icon-right" href="#/tab/social">
                        <i class="icon ion-chevron-right"></i>
                        Social Networking
                    </a>
                    <a class="item item-icon-right" href="#/tab/chapters">
                        <i class="icon ion-chevron-right"></i>
                        Chapters
                    </a>
                </div>

            </ion-content>
        </ion-view>
    </script>

    <script id="templates/resources.html" type="text/ng-template">
        <ion-view view-title="Resources">
            <ion-content class="padding">
                <h1>Additional Resources</h1>
                <div class="list list-inset">
                    <label class="item item-input">
                        <i class="icon ion-search placeholder-icon"></i>
                        <input type="text" ng-model="searchfilter" placeholder="Search" />
                    </label>
                </div>
                <div class="item item-divider">Outreach Planning Worksheets</div>
                    <ion-list>
                        <ion-item ng-repeat="item in outreachplanningworksheets | filter:searchfilter | orderBy:'name'">
                            <a class="plainLink" ng-href="{{item.link}}">{{item.name}}</a>
                        </ion-item>
                    </ion-list>
                <div class="item item-divider">Leadership</div>
                    <ion-list>
                        <ion-item ng-repeat="item in leadership | filter:searchfilter | orderBy:'name'">
                            <a class="plainLink" ng-href="{{item.link}}">{{item.name}}</a>
                        </ion-item>
                    </ion-list>
                <div class="item item-divider">Legal</div>
                    <ion-list>
                        <ion-item ng-repeat="item in legal | filter:searchfilter | orderBy:'name'">
                            <a class="plainLink" ng-href="{{item.link}}">{{item.name}}</a>
                        </ion-item>
                    </ion-list>
                <div class="item item-divider">Witnessing</div>
                    <ion-list>
                        <ion-item ng-repeat="item in witnessing | filter:searchfilter | orderBy:'name'">
                            <a class="plainLink" ng-href="{{item.link}}">{{item.name}}</a>
                        </ion-item>
                    </ion-list>
                <div class="item item-divider">Rallies</div>
                    <ion-list>
                        <ion-item ng-repeat="item in rallies | filter:searchfilter | orderBy:'name'">
                            <a class="plainLink" ng-href="{{item.link}}">{{item.name}}</a>
                        </ion-item>
                    </ion-list>
                <div class="item item-divider">Follow-Up</div>
                    <ion-list>
                        <ion-item ng-repeat="item in followup | filter:searchfilter | orderBy:'name'">
                            <a class="plainLink" ng-href="{{item.link}}">{{item.name}}</a>
                        </ion-item>
                    </ion-list>
                <div class="item item-divider">Mobilization</div>
                    <ion-list>
                        <ion-item ng-repeat="item in mobilization | filter:searchfilter | orderBy:'name'">
                            <a class="plainLink" ng-href="{{item.link}}"><h2>{{item.name}}</h2></a>
                        </ion-item>
                    </ion-list>
            </ion-content>
        </ion-view>
    </script>

    <script id="templates/gmetrics.html" type="text/ng-template">
        <ion-view view-title="Metrics">
            <ion-content class="padding">
                <div data-update-data>
                    <h1>Metrics</h1>
                    <p>Schools Coached: {{schoolsYear}}</p>
                    <p>Gospel Presentations: {{gospelpresYear}}</p>
                    <p>Gospel Presentations: {{ralliesYear}}</p>
                    <p>Gospel Presentations: {{receiveYear}}</p>
                    <p>Schools Coached: {{schoolsAlltime}}</p>
                    <p>Gospel Presentations: {{gospelpresAlltime}}</p>
                    <p>Gospel Presentations: {{ralliesAlltime}}</p>
                    <p>Gospel Presentations: {{receiveAlltime}}</p>
                </div>
            </ion-content>
        </ion-view>
    </script>

Upvotes: 1

Views: 1246

Answers (1)

Linson
Linson

Reputation: 653

Try to open the external link with window.open(url,'_blank'); by using inappbrowser

Upvotes: 1

Related Questions