1

I cannot figure out for the life of me what the getActivity method does.

I'm relatively new to programming android and I'm trying to give my app it's own small amount of storage. To do that I'm using SharedPreferences. Usually I wouldn't be copying "word for word" what's on Android Developers but, I am about to give up hope because I can't get anything to work. I can't even get the code from the website to work correctly

Here is the relevant code I have in my app.

public String activeTab = "course_reg";
private static final String DEBUG_TAG = "Gestures";
public static final String MY_PREFS_NAME = "MyPrefsFile";

Context context = getActivity();
SharedPreferences sharedPref = context.getSharedPreferences(
        MY_PREFS_NAME, Context.MODE_PRIVATE);

The code from Android Developers is below.

Context context = getActivity();
SharedPreferences sharedPref = context.getSharedPreferences(
    getString(R.string.preference_file_key), Context.MODE_PRIVATE);

Here is the website Android Developer page: Save key-value data

Every time I put getActivity() into Android Studio it says that it cannot resolve the method.

I don't know what I'm supposed to do with it and every other question I've gone to on Stack Overflow doesn't end up telling me anything.

What does getActivity do and/or what am I doing wrong.

BlueLite
  • 187
  • 1
  • 3
  • 16

4 Answers4

2

Every time I put getActivity() into Android Studio it says that it cannot resolve the method.

It's because most probably you are writing your piece of code inside class that extends Activity or AppCompatActivity and neither has getActivity() method.

getActivity() method is present in Fragment Class and it is used to get the reference to Activity to which it is attached. This method can be used to get stuff done that requires Activity context.

You can simply use get SharedPreferences without adding context before.

@Override
protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
   SharedPreferences sp = getSharedPreferences(MY_PREFS_NAME, Context.MODE_PRIVATE);
}
rvd
  • 109
  • 1
  • 3
1

Your code will work even if you replace google's code by the following :

SharedPreferences sharedPref = this.getSharedPreferences(
getString(R.string.preference_file_key), Context.MODE_PRIVATE);

This is because your activity extends the Android activity class and if you check the documentation of the Android activity class, you will see that the getSharedPreferences() method needs a Context (Context.getSharedPreferences(..), which is already extended by the Activity class (Activity extends Context class). Try substituting the above code in place of whatever you have and it should work.

Ashish
  • 209
  • 1
  • 10
0

I'm relatively new to programming android

And very probably you are new in programming since you maybe know an another programming language, but you are in the right place!

The code:

Context context = getActivity();
SharedPreferences sharedPref = 
    context.getSharedPreferences(
        MY_PREFS_NAME, 
        Context.MODE_PRIVATE
    );

Is just a part of the code. It is not there the important part, what you need, because all non beginners know what is it.

In programming (for me) a lot more usefully is to learn searching, than learn therms! In this case you should search for "getSharedPreferences example".

Than you will see, if you are in the Activity class than is not needed the getActivity. If you are in a Fragment class or whatever else, than s needed. Depends where you are at.

For example here it is not a getActivity()

And here there is an getApplicationContext()

And here you will find a tutorial how to use it

matheszabi
  • 594
  • 5
  • 16
  • It's just my first time with Java lol. It's so different. How can I tell whether I'm in a fragment class or not though? – BlueLite Feb 27 '19 at 23:11
  • I feel like that would be really obvious if I new more Java – BlueLite Feb 27 '19 at 23:12
  • @NoahBaxley you can tell if you're in a Fragment or Activity by just looking at your class definition in the file you have open. It will say either `extends Activity` or `extends Fragment` (or some other subclass of those, like `AppCompatActivity`, which "is-a" Activity) – Kyle Falconer Feb 27 '19 at 23:19
0

getActivity() is a method that belongs to the Fragment or FragmentCompat classes (check those classes in the Android API), all it does is get a reference to the context of the activity the fragment is attached to.

If you need to access only the sharedpreferences, you need to call getContext() or the keyword "this" if you're invoking it from within an Activity or Service.

On your code snippet, I'm guessing all of those are attributes or constants in your class so change it to this if using it from within an Activity or service. If you're using it in another non-android object, you'll have to access if from somewhere else (use dagger and inject the context, or have some static reference to it in the application class but this last option is not recommended).

public String activeTab = "course_reg";
private static final String DEBUG_TAG = "Gestures";
public static final String MY_PREFS_NAME = "MyPrefsFile";

Context context = getContext(); // or just Context context = this;
SharedPreferences sharedPref = context.getSharedPreferences(
        MY_PREFS_NAME, Context.MODE_PRIVATE);
Isaac Urbina
  • 1,295
  • 11
  • 21