hkjhadj1
hkjhadj1

Reputation: 886

Dial a number and come back to the application

I am trying to make an when where I provide a list of numbers and then it starts calling them one by one. So, the flow should be something like this:

for each number in list
    ask user if they want to call. If yes:
         open dialer
         call number
         come back to the app
    If no:
         break the loop

I have tried this:

public partial class MainPage : ContentPage
{
    List<string> numbers = new List<string>();
    //Consider the list is already filled with different numbers

    public MainPage()
    {
        InitializeComponent();
    }

    private async Task Button_ClickedAsync(object sender, EventArgs e)
    {
        bool answer = false;
        foreach(var number in numbers)
        {
            answer = await DisplayAlert("Question?", $"Call {number}?", "Yes", "No");
            if (answer)
            {
                var phoneDialer = CrossMessaging.Current.PhoneDialer;
                phoneDialer.MakePhoneCall(number);
            }
            else
                break;
        }
    }
}

I am using the Messaging Plugin to make the call. The problem is that when I click the button that calls the Button_ClickedAsync method, I get to call the first number in my list, after that when I end the call and return to the app, the loop doesn't continue after that. So, I have to click the button again and the loop starts over from the very first number in the list (As expected). How can I make it continue the list when I return to my app so I don't have to click the button each time?

I have also tried with Intents but I get the same result:

    private async Task Button_ClickedAsync(object sender, EventArgs e)
    {
        bool answer = false;
        foreach(var number in numbers)
        {
            answer = await DisplayAlert("Question?", $"Call {number}?", "Yes", "No");
            if (answer)
            {
                var uri = Android.Net.Uri.Parse($"tel:{number}");
                var intent = new Intent(Intent.ActionDial, uri);
                Android.App.Application.Context.StartActivity(intent);
            }
            else
                break;
        }

Upvotes: 1

Views: 143

Answers (1)

ChristiaanV
ChristiaanV

Reputation: 5541

While calling your application is getting to the background and is resumed when you hang up the call. If you don't save your state when the app is pushed to the background it will just start it like it's a fresh start.

In the Application class there are methods to override which you can use to save the state and check the state on resume. https://learn.microsoft.com/en-gb/xamarin/xamarin-forms/app-fundamentals/app-lifecycle

So my suggestion would be to store the state of the current phone number you're calling and on the resume check if there was a previous number called and continue from that number in your list.

Upvotes: 2

Related Questions