Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
382 views
in Technique[技术] by (71.8m points)

android - How to call a method in another activity

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

Here is two activity classes.

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 is main activity. Tab1Activity is sub activity and display listview. HelloAndroidActivity include Tab1Activity view.

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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();
    }
}

EDIT If 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();
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...