Search code examples
c#androidxamarinandroid-activity

Start activities from message handler



I am trying to achieve something like call activities behavior.
I have a MessageHandler that looks like this:
public class EventMessagesHandler : Handler
{
        private readonly Context _applicationContext;
        public EventMessagesHandler (Context applicationContext)
        {
            this._applicationContext = applicationContext; // not sure if this is necessary 
        }
        public override void HandleMessage(Message msg)
        {
            base.HandleMessage(msg);
            if (msg.What == Constants.DISPLAY_OUTGOING_CALL_ACTIVITY)
            {
                Intent outgoingCallIntent = new Intent(Application.Context, typeof(OutgoingAudioCallActivity));
                outgoingCallIntent.AddFlags(ActivityFlags.NewTask | ActivityFlags.ClearTask | ActivityFlags.ReorderToFront | ActivityFlags.SingleTop | ActivityFlags.ClearTop | ActivityFlags.NoHistory);
                Application.Context.StartActivity(outgoingCallIntent);
            }
            if (msg.What == Constants.DISPLAY_INCOMING_CALL_ACTIVITY)
            {
                Intent incomingCallIntent = new Intent(Application.Context, typeof(IncomingCallActivity));
                incomingCallIntent.AddFlags(ActivityFlags.NewTask | ActivityFlags.ReorderToFront);
                Application.Context.StartActivity(incomingCallIntent);
            }
        }
}

And inside my Incoming call activity I have the following code in OnClick button in order to open outgoing call activity:

Intent outgoingCallIntent = new Intent(ApplicationContext, typeof(OutgoingAudioCallActivity));
outgoingCallIntent.AddFlags(ActivityFlags.ClearTask | ActivityFlags.ClearTop | ActivityFlags.NoHistory);
ApplicationContext.StartActivity(outgoingCallIntent);
Finish();

The desired behavior is the following:
Lets call incoming call activity - "Activity A" and outgoing call activity - "Activity B".
Here are the requirements:

  1. When user clicks decline button - "A" activity should be removed from activity stack and disappear so I won't be able to open it from recent applications.

  2. When user clicks answer button - Open activity "B" from activity "A" and remove activity "A" from stack so I won't be able to Go back to activity "A" from "B" and in recent applications I will see activity "B" and not "A".

  3. When call is done (either by clicking drop call or remote endpoint dropped call) activity "B" should be finished - meaning it will not be visible in recent applications.

  4. When received DISPLAY_OUTGOING_CALL_ACTIVITY message - "B" will be opened without re-initializing it whenever user goes back to activity (either by clicking home button and open it from recent applications or just by opening recent applications and go back to activity)

I believe that most of it can be implemented using ActivityFlags but I have failed to find the right ones to use in order to get the desired result.

Any help would be great!

Thanks.


Solution

  • I was able to achieve the requirements by doing the following:
    When starting outgoing activity from handler I had the following activity flags:

    ActivityFlags.NewTask | ActivityFlags.SingleTop | ActivityFlags.ClearTop | ActivityFlags.ClearTask
    

    Opening outgoing activity from incoming call activity had the same flags as above.
    Incoming call activity had the following:

    ActivityFlags.NewTask
    

    When I had to close incoming call activity or outgoing call activity I called:

    FinishAndRemoveTask();