1

I am new to android and I have read about context in Android Documentation and in below given link,

What is 'Context' on Android?

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.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Ram Keerthy
  • 227
  • 4
  • 16
  • 2
    You can't call a method in another activity, activities are views that visible to users if you wanna to show an activity or get some data from another activity you have to use intent, so please read this: [link](https://stackoverflow.com/questions/15445182/passing-data-from-one-activity-to-another-using-bundle-not-displaying-in-secon) – FarshidABZ Jun 18 '18 at 10:04
  • Thank you for the above guidance, the given link helped me to understand about how to pass data from one activity to another. – Ram Keerthy Jun 18 '18 at 10:09

5 Answers5

1

Its impossible, you can not do it, because in static method, you can only invoke another static method,

GianhTran
  • 3,443
  • 2
  • 22
  • 42
  • In case if all the methods func1(), func2() and func3() in the class Sample1 are static methods can it be called in method function1(Context context) of class Sample2. – Ram Keerthy Jun 18 '18 at 10:04
1

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.

Aastha Doshi
  • 147
  • 1
  • 14
0

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

Zafar Hussain
  • 264
  • 2
  • 10
0

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
    }
}
-1

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;
    }
}
Parth Suthar
  • 123
  • 4