0

I need suggestion as to what is the best way to achieve this: I want to be able to run a function which requires access to my sqlite database by clicking on an option inside my preferences. I have been able to create a PreferenceFragment that properly manages my preferences.

I have been trying to find a way to directly call a function in my main activity but did not find any simple solution.

Is it possible to do such thing without a need to access main activity?

Farzad Khalafi
  • 301
  • 1
  • 8

1 Answers1

0

If main activity means the activity that is managing the fragment this is easy. You can get access to the activity managing a fragment by calling getActivity(). So lets say your main activity is called MyActivity, use the following code to call a function named myFunctionCall on it.

    MyActivity mainActivity = (MyActivity)getActivity();
    mainActivity.myFunctionCall();
Bobbake4
  • 24,509
  • 9
  • 59
  • 94
  • Would this require me to execute the preferencefragment without using intent? – Farzad Khalafi Sep 26 '12 at 20:38
  • This should be inside the preferencefragment and it will not matter how the fragment was added to the activity. Maybe post the code you are using to launch the fragment. – Bobbake4 Sep 26 '12 at 21:15