Android的 - 在调用从另一个活动的方法,无需启动新活动 [英] Android - Calling a method in one activity from another, without starting new activity

查看:98
本文介绍了Android的 - 在调用从另一个活动的方法,无需启动新活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用GreenDroid开发一个Android应用程序。应用程序仅用于测试大气压,所以这一切它包含与一个刷新按钮,三个选项卡,以及为每个标签页的活性的动作条。

所有我想要实现在一分钟是显示敬酒消息时刷新按钮pssed在动作条$ P $,但我想从内我的活动之一叫做敬酒的消息,我们将称之为Listener1Activity这是坐落在第一个选项卡的活动......这是因为Listener1Activity最终将包含一个列表,我想重装时,动作条按钮是pressed,如果我能得到它用一个简单的祝酒工作消息现在那么我可以稍后整理。

我看着意图,广播,但似乎没有适应。<​​/ P>

我的没有的希望活动开始新的每次按键pressed,我只是想在它的方法来调用并显示敬酒。

因此​​,基本上,它就像具有同时运行2活动,和一个按钮preSS在一个调用其他的方法。难道不是吗?还是我得到了那个错了吗?

SenderActivity和Listener1Activity。

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

谢谢!

史蒂芬

解决方案

如果你不希望其他活动实例化,那么这是不是这种方法的地方。如果它的共享多个活动之间的功能,为什么不为您的活动源于活动的基类。

 公共类ActivityBase扩展活动
{
公共无效showToast()
{
...
 

那么你的活动,从这个派生

 公共类MyActivity扩展ActivityBase
{
公共无效的someMethod()
{
showToast();
 

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.

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.

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

I don't want 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?

SenderActivity and 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?

Thanks!

Steven

解决方案

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.

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

Then your activities derive from this

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

这篇关于Android的 - 在调用从另一个活动的方法,无需启动新活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆