Android - calling a method in one action from another, without starting a new activity

I am developing an Android application using GreenDroid. The application is intended only for testing atm, so all it contains is an ActionBar with an update button, three tabs and activity for each of these tabs.

Everything that I am trying to achieve at the moment shows a toast message when the update button is pressed on the ActionBar, but I want the toast message to be called from one of my actions, we will call it Listener1Activity which is the activity that is on the first tab. .. this is because Listener1Activity will eventually contain the list that I want to reload when the ActionBar button is pressed, if I can make it work with a simple toast, until I can figure it out later.

I was looking for intent, broadcast, but nothing of the sort fit.

I don’t want the activity to start every time a button is pressed, I just want a call to be made in it and a toast.

In principle, this is similar to performing two actions simultaneously and pressing one button when calling a method in another. Is not it? Or I'm wrong?

SenderActivity and Listener1Activity.

In iOS, I just send NSNotification from SenderActivity and add an observer to Listener1Activity. What would be the best way to achieve this in Android?

Thank!

Stephen

+3
source share
4 answers

If you do not want another operation to be created, this is not the place for this method. If it shares functionality between several actions, why not create a base class for your actions, originating from Activity.

public class ActivityBase extends Activity
{
public void showToast()
{
...

Then your actions flow from this

public class MyActivity extends ActivityBase
{
public void someMethod()
{
showToast();
+5
source

Right If the method is static, which should probably be if this is your goal, simply name it like this:

YourClass.staticMethod(params);

If not, you need to create an object for it.

YourClass yourClass = new YourClass(constructorParams);
yourClass.method(params);

That should do it.

+3
source

I'm not sure about your question, but try, maybe this will work.

((MainActivity) activity).textViewSetText();

public void textViewSetText (String value){

    tv.setText(value);    
}

but your activity should expand MainActivity.

0
source

In addition to the static method, you cannot call any methods that are in another action!

0
source

Source: https://habr.com/ru/post/1794032/


All Articles