java Android - 从另一个活动中调用一个方法,而不启动新活动

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5086446/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 09:19:54  来源:igfitidea点击:

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

javaandroidgreendroid

提问by Steven Elliott

I'm developing an Android app using GreenDroid. The application is just for testing atm, so it all it contains is an ActionBar with a refresh button, three tabs, and an activity for each of those tabs.

我正在使用 GreenDroid 开发 Android 应用程序。该应用程序仅用于测试 atm,因此它仅包含一个 ActionBar,其中包含一个刷新按钮、三个选项卡以及每个选项卡的活动。

All I'm trying to achieve at the minute is showing a toast message when the refresh button is pressed on the ActionBar, but I want the toast message to be called from within one of my activities, we'll call it Listener1Activity which is the activity that sits in the first tab ... this is because Listener1Activity will eventually contain a list that I want to reload when the ActionBar button is pressed, if I can get it working with a simple toast message for now then I can sort out that later.

我现在想要实现的只是在 ActionBar 上按下刷新按钮时显示 Toast 消息,但我希望从我的一个活动中调用 Toast 消息,我们将其称为 Listener1Activity位于第一个选项卡中的活动......这是因为 Listener1Activity 最终将包含一个列表,我想在按下 ActionBar 按钮时重新加载该列表,如果我现在可以使用简单的 toast 消息让它工作,那么我可以解决这个问题之后。

I've looked into intents, broadcasts, but nothing seems to fit.

我已经研究了意图、广播,但似乎没有什么合适的。

I don'twant the activity starting new each time the button is pressed, I just want a method in it to call and show the toast.

希望每次按下按钮时活动都开始新的活动,我只想要其中的一个方法来调用并显示吐司。

So basically, it's just like having 2 activities running at the same time, and a button press in one calling a method in the other. Isn't it? Or have I got that wrong?

所以基本上,这就像同时运行 2 个活动,按下一个按钮调用另一个中的方法。不是吗?还是我弄错了?

SenderActivity and Listener1Activity.

SenderActivity 和 Listener1Activity。

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

在 iOS 中,我只会从 SenderActivity 发送一个 NSNotification,并在 Listener1Activity 中添加一个观察者。在 Android 中实现这一目标的最佳方法是什么?

Thanks!

谢谢!

Steven

史蒂文

回答by Rich

If you don't want the other Activity instantiated, then that's not the place for this method. If it's shared functionality between more than one Activity, why not create a base class for your activities that derives from Activity.

如果您不想实例化其他 Activity,那么这不是此方法的地方。如果它在多个 Activity 之间共享功能,为什么不为从 Activity 派生的 Activity 创建一个基类。

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

Then your activities derive from this

那么你的活动来自于此

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

回答by JMRboosties

Right. If the method is static, which it probably should be if this is your goal, just call it like this:

对。如果该方法是静态的,如果这是您的目标,它可能应该是静态的,只需像这样调用它:

YourClass.staticMethod(params);

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

如果没有,您需要为它创建一个对象。

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

That should do it.

那应该这样做。

回答by DropAndTrap

I m not sure about your question but try like this may be it will work

我不确定你的问题,但像这样尝试可能会奏效

((MainActivity) activity).textViewSetText();

public void textViewSetText (String value){

    tv.setText(value);    
}

but your activity have to extends The MainActivity.

但您的活动必须扩展 MainActivity。

回答by Carl

In addition to the staticmethod, you can not call any methods which in another activity!

除了静态方法之外,您不能调用另一个活动中的任何方法!