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.
Related
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
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
My problem is similar to this question Can't make static reference to non-static method ( Android getApplicationContext() )
I need to get the context of the SherlockFragmentActivity in order to access the database class. I tried the solution in this link above, but it did not work.
Question 1: How do I get the context in the code below.
Question 2: I get an error that forces me to use 'static' instead of public for the application context variable. I know that static is for a variable that does not change. However, this variable will change each time a tab is clicked on. Also, 'static' variables are not required for the database class. I'm confused as to why I need a static variable here.
my SherlockFragmentActivity:
public class FragmentTabs extends SherlockFragmentActivity {
TabHost mTabHost;
TabManager mTabManager;
static FragmentTabs appState;
TabSwitchIdDatabase tsid = new TabSwitchIdDatabase(this);
#Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(SampleList.THEME); // Used for theme switching in samples
super.onCreate(savedInstanceState);
appState = ((FragmentTabs)getApplicationContext());
//.... more code
}
public static class TabManager implements TabHost.OnTabChangeListener {..// see code snipit below....}
}
Where i need to put the context
public static class TabManager implements TabHost.OnTabChangeListener {
//... more code
static class DummyTabFactory implements TabHost.TabContentFactory {
//... more code
#Override
public void onTabChanged(String tabId) {
TabInfo newTab = mTabs.get(tabId);
System.out.println(tabId);
tsid.open();// broken , scoping problem
Boolean x =tsid.tabExists(0);
String tabIDfromDatabase = tsid.getTab(0);// broken , scoping problem
tsid.close();// broken , scoping problem
}
}
}
Do you have a constructor for your DummyTabFactory?
Pass the context as an argument to it.
Assign the passed context to a local variable.
So your code should look like something like this:
public class FragmentTabs extends SherlockFragmentActivity {
DummyTabFactory mDummyTabFactory = new DummyTabFactory(getApplicationContext());
static class DummyTabFactory implements TabHost.TabContentFactory {
private Context mContext;
public DummyTabFactory(Context context) {
super(fm);
mContext = context;
}
}
}
Now you can use mContext to access your app's resources.
Are you sure the problem is related to the SherlockFragmentActivity itself?
Have you checked (for example) that you have specified android:name=".MyApplication" in your AndroidManifest.xml file?
I have a class that is currently extending Activity and I have methods like findViewById, ArrayAdapter etc.
I want to turn it into an independent class but all the above methods become undefined. What is the problem? Shouldn't importing the classes be enough? For eg, I import android.view.View for findViewById but it still makes no difference.
Please advise.
you should pass the instance of your Activity to your Second Class on the constructor like this :
In your Activity Instanciate your Class like this :
MyClass instance = new MyClass(this);
And in your second Class , the constructor will be like this :
public class MyClass {
public Activity activity;
//.... other attributes
public MyClass( Activity _activity){
this.activity = _activity;
//other initializations...
}
}
and then when you want to use the findViewById() method , you can do like this :
EditText txt = (EditText)this.activity.findViewById(R.id.txt);
if you want to call any function that belongs to Activity then only thing you need to have is context of the Activity.
eg.
class A extends Activity {
Context ctx;
void onCreate(Bundle b)
ctx = this;
B bob = new B(ctx);
}
}
Here is class B.
class B {
B (Activity a) {
a.anyFunctionOfActivity();
}
}
findViewById is non-static public method of the Activity Class ,so it only be available for a Activity object. Therefore, import android.view.View and android.app.Activity won't make it available. To make it available, you could pass around a Actvity object - usually a this point in your activity. However, notice that you should always update your View in the UI thread.
Please try the following:
public static View getLayoutByRes(#LayoutRes int layoutRes,#Nullable ViewGroup viewGroup)
{
final LayoutInflater factory = getSystemService(Context.LAYOUT_INFLATER_SERVICE);
return factory.inflate(layoutRes, viewGroup);
}
TextView tv = (TextView) getLayoutByRes(R.layout.xxx ,Null).findViewById(R.id.editd);
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...
}
}