Start a new activity and wait for it to end

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;
        }

Answers

  • jezhjezh Member, Xamarin Team Xamurai

    @ap13371
    I have implement the effect that you need.
    When you return from the activity , the activity call the method: OnResume(), so you can take advantage of this characteristic.

        protected override void OnResume()
        {
            base.OnResume();          
            if (currentIndex!=0)  //  the first time is not call the method ,wait button to fire.
            {
                if (currentIndex < numbers.Count)
                {
                    string number = numbers[currentIndex];
                    currentIndex++;   
    
                    var uri = Android.Net.Uri.Parse($"tel:{number}");
                    var intent = new Intent(Intent.ActionDial, uri);
                    Android.App.Application.Context.StartActivity(intent);
                }
            }
        }
    

    Details are attached. (MainActivity)
    Hope can help you.

Sign In or Register to comment.