Reputation: 227
I am new to android and I have read about context in Android Documentation and in below given link,
If suppose I have a class and it contains some methods in it, for instance consider the below given code snippet.
Sample1.java
class Sample1 extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_login);
Sample2.function1(Sample1.this);
}
public void func1()
{
//...
}
public void func2()
{
//...
}
public void func3()
{
//...
}
}
Sample2.java
class Sample2 extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_login);
}
public static void function1(Context context){
//can I access all the public methods present in sample1 class
}
}
Please do pardon me if the doubt is wrong. I am trying to understand the basics. Any help would be appreciable and thanks in advance.
Upvotes: 1
Views: 253
Reputation: 48
if both are static function then it is possible, Static method is inherited in subclass but it is not polymorphism.
class Sample1 extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_login);
Sample2.function1(Sample1.this);
}
public static void func1()
{
//...
}
public static void func2()
{
//...
}
public staic void func3()
{
//...
}
}
after that you use `enter code here`
class Sample2 extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_login);
}
public static void function1(Context context){
//can I access all the public methods present in sample1 class
}
}
Upvotes: 0
Reputation: 147
Yes absolutely you can do this even if you use a default java class then also you can call the function of that class into another activity.Here there be no conflict if you use the keyword static.
Upvotes: 1
Reputation: 268
As the name suggests, it's the context of current state of the application/object. It lets newly-created objects understand what has been going on. Typically you call it to get information regarding another part of your program (activity and package/application).
You can get the context by invoking getApplicationContext(), getContext(), getBaseContext() or this (when in a class that extends from Context, such as the Application, Activity, Service and IntentService classes).
Typical uses of context:
Creating new objects: Creating new views, adapters, listeners:
TextView tv = new TextView(getContext()); ListAdapter adapter = new SimpleCursorAdapter(getApplicationContext(), ...); Accessing standard common resources: Services like LAYOUT_INFLATER_SERVICE, SharedPreferences:
context.getSystemService(LAYOUT_INFLATER_SERVICE) getApplicationContext().getSharedPreferences(name, mode); Accessing components implicitly: Regarding content providers, broadcasts, intent
getApplicationContext().getContentResolver().query(uri, ...);
Upvotes: 0
Reputation: 123
Yes you can. Consider below your Sample2.java file.
class Sample2 extends AppCompatActivity {
public static Sample2 sample2;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_login);
sample2 = this;
}
public static Sample2 function1(Context context){
//can I access all the public methods present in sample1 class
return sample2;
}
}
Upvotes: -1
Reputation: 3711
Its impossible, you can not do it, because in static method, you can only invoke another static method,
Upvotes: 1