danielsogl / awesome-cordova-plugins

Native features for mobile apps built with Cordova/PhoneGap and open web technologies. Complete with TypeScript support.
https://danielsogl.gitbook.io/awesome-cordova-plugins/
Other
2.38k stars 2.43k forks source link

Problems with minimising the App. #4711

Closed damianlluch closed 2 months ago

damianlluch commented 4 months ago

Hi, I'm having a problem with an application I've developed, and alternatively I've used both this plugin and the Cordova plugin, and in both of them it happens (in all Android), that when I'm in the home (login or main screen post-login) and I press the back button, and I have a whitescreen.

I have tried many ways, this is the last one I have tried. I have to click twice on the back button, in order to minimize. The first time, it goes blank.

import { Component, OnDestroy, OnInit } from '@angular/core';
import { InAppBrowser } from '@awesome-cordova-plugins/in-app-browser/ngx';
import { NavigationService } from './navigation.service';
import { Platform } from '@ionic/angular';
import { App } from '@capacitor/app';

@Component({
  selector: 'app-home',
  templateUrl: 'home.page.html',
  styleUrls: ['home.page.scss'],
})
export class HomePage implements OnInit, OnDestroy {
  constructor(
    private iab: InAppBrowser,
    private navigationService: NavigationService,
    private platform: Platform,
  ) {}

  ngOnInit() {
    this.iab.create('https://www.saem.com.ar/ingreso/?next=/', '_blank', {
      hideurlbar: 'yes',
      hidenavigationbuttons: 'yes',
      toolbar: 'no',
      clearcache: 'no',
      clearsessioncache: 'no',
      location: 'no'
    });

    this.platform.backButton.subscribeWithPriority(9999, async () => {
      const previousUrl = this.navigationService.getPreviousUrl();
      if (!previousUrl || previousUrl == 'https://www.saem.com.ar/portal/' || previousUrl == 'https://saem.com.ar/ingreso/?next=/') {
        console.log('Minimizar app', previousUrl);
        await App.exitApp();
      } else {
        console.log('Navegar atrás', previousUrl);
        await this.navigationService.navigateBack();
      }
    });
  }

  ngOnDestroy() {
    this.platform.backButton.unsubscribe();

    document.removeEventListener('backbutton', this.handleBackButton);
  }

  private handleBackButton = async () => {
    const previousUrl = this.navigationService.getPreviousUrl();
    if (previousUrl && previousUrl !== 'https://www.saem.com.ar/portal/' && previousUrl !== 'https://saem.com.ar/ingreso/?next=/') {
      console.log('entroe');
      await this.navigationService.navigateBack();
    } else {
      console.log('Salgo de la app');
      await App.exitApp();
    }
  };

}

import { Injectable } from '@angular/core';
import {NavigationEnd, Router} from '@angular/router';
import {filter} from 'rxjs/operators';

@Injectable({
  providedIn: 'root'
})
export class NavigationService {
  private history: string[] = [];

  constructor(private router: Router) {
    this.router.events.pipe(
      filter(event => event instanceof NavigationEnd)
    ).subscribe((event: NavigationEnd) => {
      this.addUrl(event.urlAfterRedirects);
    });
  }

  addUrl(url: string) {
    this.history.push(url);
  }

  getPreviousUrl(): string | null {
    this.history.pop();
    return this.history.length > 0 ? this.history[this.history.length - 1] : null;
  }

  async navigateBack() {
    console.log('entroe2')
    await this.router.navigateByUrl(this.getPreviousUrl() || 'https://www.saem.com.ar/portal/' || 'https://saem.com.ar/ingreso/?next=/', {replaceUrl: true});
  }
}

Any suggestions?

Thanks

github-actions[bot] commented 2 months ago

There has been no recent activity and this issue has been marked inactive.