-1

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")

tborges
  • 117
  • 1
  • 8
  • Possible duplicate of [How can "this" of the outer class be accessed from an inner class?](https://stackoverflow.com/questions/2731719/how-can-this-of-the-outer-class-be-accessed-from-an-inner-class) – payloc91 Feb 25 '18 at 16:34

5 Answers5

0

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

prakash421
  • 139
  • 1
  • 8
  • Do not do this. It is well known that `static` Context is the first source of memory leaks in `Android`. Even worse, reference it from outside the class... – payloc91 Feb 25 '18 at 16:38
0

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);
SQLiteNoob
  • 2,958
  • 3
  • 29
  • 44
0

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);

    }
}
Mithun Sarker Shuvro
  • 3,902
  • 6
  • 32
  • 64
0

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);
-1

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

prakash421
  • 139
  • 1
  • 8