本文介绍了如何在另一个活动中调用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于Android实现程序的Activity之间通信的问题.

I have a question about communication between activities on implementation program of Android.

这里有两个活动类.

public class HelloAndroidActivity extends TabActivity{
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Resources res = getResources();
        TabHost tabHost = getTabHost();
        TabHost.TabSpec spec;
        Intent intent;

        intent = new Intent().setClass(this, Tab1Activity.class);
        spec = tabHost.newTabSpec("Tab1").setIndicator(
          "Tab1", res.getDrawable(R.drawable.ic_tab_icon))
          .setContent(intent);
        tabHost.addTab(spec);
    }
}

_

public class Tab1Activity extends ListActivity {
    private ArrayList<String> list = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        list = new ArrayList<String>();
        setListAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, list));

        addColumn("one");
        addColumn("two");
        addColumn("three");
    }

    public void addColumn(String s){
        list.add(new String(s));
    }
}

HelloAndroidActivity 是主要活动.Tab1Activity 是子活动并显示列表视图.HelloAndroidActivity 包含 Tab1Activity 视图.

HelloAndroidActivity is main activity.Tab1Activity is sub activity and display listview.HelloAndroidActivity include Tab1Activity view.

我想做的是从 HelloAndroidActivity 调用 addColumn 方法,因为 HelloAndroidActivity 已添加到 TwitterUserStreamAdapter 等新功能中.如果android收到来自互联网的消息,应用程序向 Tab1Activity 发送消息.

What I want to do is calling addColumn method from HelloAndroidActivity,because HelloAndroidActivity is added to new function like TwitterUserStreamAdapter.If the android receive messages from internet,application send message to Tab1Activity.

但是,我不知道如何实现活动之间的通信.

However, I don't know how to implement communication between activities.

推荐答案

你可以使用intent在activity之间传递数据,你可以把它放在额外的intent中:

You can pass data between activities using intent, you could put it in the extras with the intent:

HelloAndroidActivity

intent.putExtra("callX", true);

Tab1Activity

Bundle extras = getIntent().getExtras();
if (extras != null) {
    boolean callX = extras.getBoolean("callX");
    if(callX) {
       X();
    }
}

编辑如果您需要事件/监听器机制,它可能大致是这样的(尚未编译,但应该给您一个想法):

EDITIf you need to event/listener mechanism it could be roughly like this(haven't compiled this, but should give you an idea):

public inerface MyEventListener {
    abstract void handleMyEvent();
}

public class Tab1Activity implements MyEventListener {

    public void handleMyEvent() {
        /*...*/
    }

    protected void onCreate(Bundle savedInstanceState) {
         /*...*/
         HelloAndroidActivity.addListener(this);
    }

    protected void  onDestroy() {
         /*...*/
         HelloAndroidActivity.removeListener(this);
    }
}


public class HelloAndroidActivity {
    static ArrayList<MyEventListener> listeners = new ArrayList<MyEventListener>();
    public static void addListener(MyEventListener listener) {
        listeners.add(listener);
    }
    public static void removeListener(MyEventListener listener) {
        listeners.remove(m);
    }

    public static void onEvent() {
        for(MyEventListener m : listeners) {
            m.handleMyEvent();
        }
    }
}

这篇关于如何在另一个活动中调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-08 22:02