0

I am building an app using location api, when a user click on a button, I want to get directions on the current activity so that I can proceed to tasks. Here's the error I am getting. Calling startActivity() from outside of an Activity context requires the
FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?

I understand we can use addFlags(Intent.FLAG_ACTIVITY_NEW_TASK to get around the error. BUT the issue is I only want results from that intent to be displayed on the current activity.

Sample code. button click

   public void getDirections() {
        if (mState.getOrder() != null) {
            Uri gmmIntentUri;
            if (Order.PICKING_UP.equals(mState.getOrder().status)) {
                gmmIntentUri = Uri.parse("google.navigation:q="
                        + mState.getOrder().pickup.latitude + "," + mState.getOrder().pickup.longitude);
            } else {
                gmmIntentUri = Uri.parse("google.navigation:q="
                        + mState.getOrder().dropoff.latitude + "," + mState.getOrder().dropoff.longitude);
            }
            Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
            mapIntent.setPackage("com.google.android.apps.maps");
            //mapIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // done to resolve Calling startActivity() from outside of an Activity context issue
            mContext.startActivity(mapIntent);
        }
    }
// context 
mContext = context.getApplicationContext() == null ? context : context.getApplicationContext();
Kose
  • 37
  • 9
  • Check this.. https://stackoverflow.com/questions/37768604/how-to-use-startactivityforresult – Tanmoy Jun 26 '20 at 13:15

1 Answers1

0

If you want to get data from Intent object you should method startActivityForResult.

      Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
        mapIntent.setPackage("com.google.android.apps.maps");
        //mapIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // done to resolve Calling startActivity() from outside of an Activity context issue
        mContext.startActivityForResult(mapIntent);
Blazej Kita
  • 99
  • 1
  • 1
  • 10