Android: why must use getBaseContext() instead of this

getApplicationContext () returns the application context of the entire application life cycle,when application will destroy then it will destroy also. this the context returns the current context of the activity, belong to the activity, the activity is destroyed then it will destroy also.but in your case it will refers to the Spinner instance because we …

Read more

Difference between getContext() and requireContext() when using fragments

getContext() returns a nullable Context. requireContext() returns a nonnull Context, or throws an exception when one isn’t available. If your code is in a lifecycle phase where you know your fragment is attached to a context, just use requireContext() to get a Context and also keep static analyzers happy about potential NPE issues. If your …

Read more

How do I view Android application specific cache?

You may use this command for listing the files for your own debuggable apk: adb shell run-as com.corp.appName ls /data/data/com.corp.appName/cache And this script for pulling from cache: #!/bin/sh adb shell “run-as com.corp.appName cat ‘/data/data/com.corp.appNamepp/$1’ > ‘/sdcard/$1′” adb pull “/sdcard/$1” adb shell “rm ‘/sdcard/$1′” Then you can pull a file from cache like this: ./pull.sh cache/someCachedData.txt …

Read more

Diffinitive rules for using Android’s getBaseContext, getApplicationContext or using an Activity’s “this”

The getBaseContext() is the method of ContextWrapper. And ContextWrapper is, “Proxying implementation of Context that simply delegates all of its calls to another Context. Can be subclassed to modify behavior without changing the original Context.” (as per javadocs) So this is used to delegate the calls to another context.

Get application context from non activity singleton class

Update: 06-Mar-18 Use MyApplication instance instead of Context instance. Application instance is a singleton context instance itself. public class MyApplication extends Application { private static MyApplication mContext; @Override public void onCreate() { super.onCreate(); mContext = this; } public static MyApplication getContext() { return mContext; } } Previous Answer You can get the the application context …

Read more