-2

I'm developing an android application that is dependent on maps and location. I have ActivityA that contains fragmentA.

I have an alert that request the user to turn on location service inside fragmentA. When the user accept to turn on the service, I want my code to continue running, otherwise finish() the app. So I should use OnActivityResult() to know what did the user pressed in the alert. My problem is that when the user accept to turn on Location, the function OnActivityResult() is launched in the activity and not in the fragment. I read about this problem, people are suggesting to call startActivityForResult(...) rather than startActivity(...). But I can't start the activity from an inside fragment because this will lead to crash problems regarding data that are in the intent. So any one has any suggestions ? This is the function that requests Location in fragmentA.

  • 1
    See https://stackoverflow.com/questions/6147884/onactivityresult-is-not-being-called-in-fragment. – ADM May 18 '18 at 13:10
  • @ADM I saw this post and I think they're talking about getting results from another activity, my problem is getting result from same activity. – Ahmad Alibrahim May 18 '18 at 13:15
  • 1
    `But I can't start the activity from an inside fragment because this will lead to crash problems ` ? Whats this suppose to mean ? While you can use `startActivityForResult()` from fragment ? – ADM May 18 '18 at 13:19
  • I'm starting the activity (ActivityA) from another activity (ActivityB), and sending data in the intent and getting them in `ActivityA`, so when i call `startActivityForResult()` from fragment, the data inside the intent are nulls. I can know if I'm starting the activity (ActivityA) from `ActivityB` or from the fragment (fragmentA) but I'm searching on a simpler way. – Ahmad Alibrahim May 18 '18 at 13:26

1 Answers1

0

Intent intent = new Intent( getActivity(), yourClass.class ); getActivity().startActivity(intent);

StuckPixel
  • 111
  • 8