getActivity() returns null in Fragment function

I have a fragment (F1) with a public method like this public void asd() { if (getActivity() == null) { Log.d(“yes”,”it is null”); } } and yes when I call it (from the Activity), it is null… FragmentTransaction transaction1 = getSupportFragmentManager().beginTransaction(); F1 f1 = new F1(); transaction1.replace(R.id.upperPart, f1); transaction1.commit(); f1.asd(); It must be something that … Read more

Call getLayoutInflater() in places not in activity

What does need to be imported or how can I call the Layout inflater in places other than activity? public static void method(Context context){ //this doesn’t work the getLayoutInflater method could not be found LayoutInflater inflater = getLayoutInflater(); // this also doesn’t work LayoutInflater inflater = context.getLayoutInflater(); } I am able to call getLayoutInflater only … Read more

Difference between Activity Context and Application Context

This has me stumped, I was using this in Android 2.1-r8 SDK: ProgressDialog.show(getApplicationContext(), ….); and also in Toast t = Toast.makeText(getApplicationContext(),….); using getApplicationContext() crashes both ProgressDialog and Toast …. which lead me to this question: What is the actual differences between a activity context and application context, despite sharing the wording ‘Context’? 7 Answers 7

What’s the difference between the various methods to get an Android Context?

In various bits of Android code I’ve seen: public class MyActivity extends Activity { public void method() { mContext = this; // since Activity extends Context mContext = getApplicationContext(); mContext = getBaseContext(); } } However I can’t find any decent explanation of which is preferable, and under what circumstances which should be used. Pointers to … Read more

How to get package name from anywhere?

I am aware of the availability of Context.getApplicationContext() and View.getContext(), through which I can actually call Context.getPackageName() to retrieve the package name of an application. They work if I call from a method to which a View or an Activity object is available, but if I want to find the package name from a totally … Read more

Calling startActivity() from outside of an Activity context

I have implemented a ListView in my Android application. I bind to this ListView using a custom subclass of the ArrayAdapter class. Inside the overridden ArrayAdapter.getView(…) method, I assign an OnClickListener. In the onClick method of the OnClickListener, I want to launch a new activity. I get the exception: Calling startActivity() from outside of an … Read more

getApplication() vs. getApplicationContext()

I couldn’t find a satisfying answer to this, so here we go: what’s the deal with Activity/Service.getApplication() and Context.getApplicationContext()? In our application, both return the same object. In an ActivityTestCase however, mocking the application will make getApplication() come back with the mock, but getApplicationContext will still return a different context instance (one injected by Android). … Read more

Using Application context everywhere?

In an Android app, is there anything wrong with the following approach: public class MyApp extends android.app.Application { private static MyApp instance; public MyApp() { instance = this; } public static Context getContext() { return instance; } } and pass it everywhere (e.g. SQLiteOpenHelper) where context is required (and not leaking of course)? 9 s … Read more