How to pass the context of some activity as an argument - android

I want to know how can I send the context of an activity from ClassA.java to MainAddingItems.java.
I can pass the MainActivity.class as the argument but I am not able to pass the context.
Here is some of the code of ClassA calling the constructor of MainAddingItems
new MainAddingItems(MainActivity.class,"MainActivity Clicked",R.id.activity_main_linearLayout,"Profile Acitvity")

It will work ..make sure that you declerad it as public and static like below
Public static Context context ;
In your main activity
You can use any where that context in whole project

You can do this. If your constructor is:
MainAddingItems(Context context) {...}
Then from Activity1 you can just do:
Context context = Activity1.this; // Or getApplicationContext() or View.getContext() or whatever context you want
MainAddingItems(context);

Make your MainAddingItem class's constructor as this,
class MainAddingItem {
Context context;
MainAddingItem (Context context, rest of the parameters){
this.context = context;
}
}
Use the above constructor and pass the activity's context in the constructor's parameter.
new MainAddingItem (this, rest of the parameters);

Actually you are passing the context here . In android context is the current state of application . As an example if you are in a activity, then the context is the activity class itself , same for service and any other component of android
Let me give another example ,
Just try to show a toast message in onCreate method of any activity , considering your activity name is SimpleActivity you may write something like this
Toast.makeText(this "Understating context",Toast.LENGTH_LONG).show();
Or you may write the following
Toast.makeText(SimpleActivity.this "Understating context",Toast.LENGTH_LONG).show();
The first parameter of makeText method is context , and it works if you simply pass the class .
This is how you will pass activity as an argument
public SimpleClass{
AppCompatActivity mActivity;
public SimpleClass(AppCompatActivity mActivity){
this.mActivity = mActivity;
}
}
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SimpleClass newSimpleClass = new SimpleClass(MainActivity.this);
}
}

Just make Context as static like in MainActivity.
Public static Context contextToUse;
You can use like below :
MainActivity.contextToUse
Please let me know ,if it is helpful to you

Related

UnityPlayerActivity getContext() substitution

Is there an alternative to the getContext in UnityPlayerActivity?
I want the code as shown below.
Activity.getContext()
answer from here
using (var actClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) {
playerActivityContext = actClass.GetStatic< AndroidJavaObject >("currentActivity");
}
or you can use code below from here
public class Main extends UnityPlayerActivity {
public static Context mContext;
#Override
protected void onCreate(Bundle bundle)
{
super.onCreate(bundle);
mContext = this;
}
}
getContext() is not defined in an Activity. It's used in a View to get a reference to the enclosing context(an Activity).
Please read below Answer .I hope it will helps you.
Difference between getContext() , getApplicationContext() , getBaseContext() and "this"

is using "this" a form of polymorphism?

i have a class called LauncherActivity that extends FragmentActivity as shown:
public class LauncherActivity extends FragmentActivity {
public Context mContext;
private Common mApp;
public Activity mActivity;
mContext = this;
mActivity = this;
mApp = (Common) mContext.getApplicationContext();
i have read about polymorphism and gotten to know the main use is to use parent references to access child objects.Since LaucherActivity is the child class and FragmentActivity is the parent class,and FragmentActivity is a child class of the Context class in reference to the android documentation, using this as shown below is allowed ? is it a form of polymorphism?:
mContext = this;
mActivity = this;
am presume this refers to the current object which is LauncherActivity ?
Usually , this keyword is uses to say that , refers to the context of the current activity.

How to get the current Activity context for a non-activity class, statically

I have a non-activity class that needs to launch a new activity. Right now I just pass the current context as a parameter, but I would like to access the context statically if possible. I've already tried creating a new application (MyApplication extends Application), both as a new application and the main application, and neither worked. Any suggestions?
Current Code:
public class SharedFunctions {
public static void doSomething(Context context){
Intent i = new Intent(context, NextActivity.class);
context.startActivity(i);
}
}
The cleaner way to do it is to pass in a Context to each method. It's more typing, but it helps to make sure you're not leaking the reference.
Of course, if you really need static reference to it, you can just keep a static member in your SharedFunctions class and set it for each Activity.
onResume() and onPause() may be good places to set/clear it, but depending on your needs, you might want to change it. Just try not to keep references to old Activities.
public class SharedFunctions{
private static Context context;
public static void setContext(Context ctx){
context = ctx;
}
public static void doSomething(){
context.someContextFunction();
}
}
In each Activity:
protected void onResume(){
SharedFunctions.setContext(this);
}
protected void onPause(){
SharedFunctions.setContext(null);
}
create this class:
public class MyApplication
extends Application
{
private static Context context;
#Override
public void onCreate()
{
super.onCreate();
context = getApplicationContext();
}
public static Context getContext()
{
return context;
}
}
after that you must add this class to field name in application (Manifest)
<application
android:name="yourPackageName.MyApplication"
........
</application >
As a result you can call MyApplication.getContext() anywhere in your application and get the context.
hope, I help you.

In Android, is there a more elegant way to retrieve application context inside a inner class?

In Android, is there a more elegant way to retrieve application context inside a inner class rather than passing context as a parameter?
public class MainActivity extends Activity {
class SeekBarChangeListener implements SeekBar.OnSeekBarChangeListener
{
private Context context;
private TextView distanceTextView;
public SeekBarChangeListener(Context context, TextView distanceTextView) {
this.context = context;
this.distanceTextView = distanceTextView;
}
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
}
Seeing as its an inner class of an Activity, you could use:
this.context = MainActivity.this.getApplicationContext();
call MainActivity.this from the inner class and it will give you the Activity context object.
You should not use the applicationContext i.e. calling getApplicationContext() unless you really need it, but from your example code you have, Activity context should be enough

How to use setContentView(int) from class which does not extend Activity

I need to call the setContentView(int) from my main Activity from another class which does not extends Activity.
In my custom class I've got the private Context context; var that is passed from the Activity in the Constructor but I can't figure out how to acces the Activity methods using the context variable.
If your context is an instance of Activity class, simple class cast should work:
Activity a = (Activity) context;
a.setContentView(R.layout.your_layout);
One solution (may not be the most elegant) is to pass the calling activity to the other class, not just the context.
You would have to pass in a reference to the Activity you're using.
Something like this
class ActivityA extends Activity{
#Override
public void onCreate(Bundle state){
super.onCreate(state);
ClassA myclass = new ClassA(this);
}
}
And then Class A would have:
class ClassA {
public ClassA(Activity yourActivity){
... Get your view here ....
yourActivity.setContentView(view);
... do more things...
}
}

Categories

Resources