-3

So I know there are probably 100 threads about this topic already. Yes I tried to read them but i can't for my life understand the concept of context in Android :(

I want to call a non-static method from "MainActivity" in another activity.

This is where i try to call the non-static method in the other activity:

public void removeSelected() {
        for(A4Dialogpojo item : selectedItems) {
            alCustom.remove(item);
          MainActivity.saveCalData(); //This line gives me the compile error
    }

This is the non-static method in MainActivty that i want to call from the other activity:

  public void saveCalData(){
        SharedPreferences sharedPreferences = getSharedPreferences("shared preferences", MODE_PRIVATE);
        SharedPreferences.Editor editor9 = sharedPreferences.edit();
    Gson gson = new Gson();
    String json = gson.toJson( HomeCollection.date_collection_arr);
    editor9.putString("task list", json);
    editor9.apply();
    }

So any ideas? How could I use "context" in this case?

  • 1
    I'm not familiar with Android, but you need to either instantiate an object of type `MainActivity` and call that object's method (e.g. `new MainActivity().saveCalData();`), or make `saveCalData()` a `static` method. – RaminS Jul 27 '19 at 21:57
  • I am 73.5% sure that "context", android specific, is what I need here :/ – Daniel Andersson Jul 27 '19 at 21:58
  • 1
    *"non-static method foo() cannot be referenced from a static context"* is a default Java compile-time error message, as you can see [here](https://ideone.com/9OZehH). – RaminS Jul 27 '19 at 22:00
  • sure but the compiler don't tell you how to solve the problem does it :) – Daniel Andersson Jul 27 '19 at 22:01
  • 1
    Point being that it's not referring to some Android-specific thing called *"context"*. – RaminS Jul 27 '19 at 22:03
  • yeah i know but there's this thing called context in android that i feel would be very useful here, my limited understanding but maybe im wrong – Daniel Andersson Jul 27 '19 at 22:08
  • I'm voting for reopening this as not a duplicate of "how to call non-static method..." and to close it as a duplicate of https://stackoverflow.com/questions/19666572/how-to-call-a-method-in-another-activity-from-activity/39578803 – m.antkowicz Jul 27 '19 at 22:22
  • Using names like `name1`, `name2` commonly bad practice. Use Java's naming convention, [like this](https://en.wikipedia.org/wiki/Naming_convention_(programming)#Java). Format your code. Remove all code that not relevant to this question. It's all help us to understand you faster. – Mikhail Ionkin Jul 27 '19 at 22:38
  • @DanielAndersson - the fundamental issue is whether you have an object ('non-static') or not ('static') to affect. This in turn relies on knowing the difference between 'class' and 'object'. –  Jul 28 '19 at 02:43

3 Answers3

2

Why do you need to keep this method in MainActivity? If it is common utility just put this to some helper class (that can even be a singleton) and have an instance of this in both Activity classes

Another approach would be to return a value from "child" Activity to the MainActivity (consider also using startActivityForResult method) and react for this properly calling proper method

As far as I understand you should not operate on Activity instances since the Activity has it's own lifecycle and is being handled independently

m.antkowicz
  • 13,268
  • 18
  • 37
  • 1
    I solved it. I re-wrote the method-call like "MainActivity.saveCalData(context);" and changed the method with context as a parameter and so it could use a SharedPref in a static-method :) – Daniel Andersson Jul 27 '19 at 22:32
1

Yes I tried to read them but i can't for my life understand the concept of context in Android :(

The compiler error message is referring to "context" (the English noun), not Context (the Java class in the Android SDK).

I want to call a non-static method from "MainActivity" in another activity.

That is not really supported in Android. Activities are independent of each other.

If these activities are that closely coupled, they should be one activity, perhaps with two fragments. Or, use a common singleton object as a repository that handles your data storage, with each activity talking to that one object.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
-2

You can't call nonsatic method without creating or getting an instance of the class

new MainActivity().saveCalData(); 

off course you have to consider proper way of instantination, you may not have default constructor etc, but its the OOP basics which is usefull to understand

Ludov Dmitrii
  • 425
  • 5
  • 9
  • 4
    Do not create an instance of an `Activity` yourself. – CommonsWare Jul 27 '19 at 22:01
  • Okay will this not override the real running MainActivity or something? – Daniel Andersson Jul 27 '19 at 22:02
  • This is not a wise or correct answer given the context. Consider deleting. – Hovercraft Full Of Eels Jul 27 '19 at 22:03
  • @CommonsWare I dont suggest creation, Its just explaination why its not working. – Ludov Dmitrii Jul 27 '19 at 22:05
  • 2
    @LudovDmitrii: "I dont suggest creation" -- `new MainActivity()` creates an instance of the `MainActivity` class. – CommonsWare Jul 27 '19 at 22:06
  • @LudovDmitrii dude you are giving example of creating Activity and saying that you are not suggesting creation... :D – m.antkowicz Jul 27 '19 at 22:06
  • @m.antkowicz have you been reading my answer, or any provided code you considering as suggestion to copy-paste? – Ludov Dmitrii Jul 27 '19 at 22:09
  • 2
    @LudovDmitrii The question is directly about how to run a method from one Activity on another. Your answer not only doesn't provide a way how to resolve this but also **suggest** invalid way to do this – m.antkowicz Jul 27 '19 at 22:11
  • @m.antkowicz yes it does not provide solution, it is not have to. It's enough to be usefull. Sometimes push a bit hurther is better than posting just blind solution, which i don't see by now, but what I see is not comprehending OOP basics – Ludov Dmitrii Jul 27 '19 at 22:23
  • If you want to provide useful information which does not answer the question you should use a comment, not an answer. – takendarkk Jul 28 '19 at 23:41
  • @takendarkk contributor can comment questions only after his score is > 50 – Ludov Dmitrii Jul 29 '19 at 08:14
  • Yes, and your reputation is > 50 so you can comment. And the OP can always comment on their own question regardless of reputation. – takendarkk Jul 29 '19 at 11:21
  • @takendarkk It became >50 yesterday, after(not not because of it obviously, but after) I posted this. What is OP? And what in it for me if this is not my question. This is becoming ridiculous. I wish to observe this kind of activity in solving not easy questions, like this https://stackoverflow.com/questions/57228391/runtime-polymorphism-in-kotlin/57229268#57229268 for example – Ludov Dmitrii Jul 29 '19 at 15:30