从其他活动的Andr​​oid调用方法 [英] Android call method from an other activity

查看:172
本文介绍了从其他活动的Andr​​oid调用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了一堆的帖子,文章和一切,但我不能得到解决..
我做一个Android应用程序..
我开始第一次活动(第一类)。然后我对第二个活动一个按钮,我加载它是这样的:

I have read a bunch of posts,articles and everything but i cant get a solution.. i make an android app.. i start the first activity(first class). then i have a button for the second activity which i load it like this:

Intent i=new Intent(firstactivity.this,secondactivity.class);
startActivity(i);

在上单击事件到一个按钮,我想打电话给从第​​一个活动的方法之一第二项活动:

in the second activity on click event to a button i want to call one method from the first activity:

firstactivity f1= new firstactivity();
f1.MyMethod("my string goes here")

当我运行该应用程序crashes..i尝试尝试捕捉exeption我打印到exeption这是空一次错误......我不能得到它的工作.....

when i run this the application crashes..i tried the try catch exeption and i printed to exeption which is one error for null... i cant get it to work.....

在此先感谢!

PS有没有什么办法来启动第二个活动,并在第一个活动的布局更新一些值(一个TextView)?只有在负载不更新,但也有例如定时器的第二个活动,每5秒的setText到一个TextView的第一项活动的布局..任何解决方案(非此即彼我问大约高于法)将pciated AP $ p $

PS is there any way to start the second activity and update some values (in a textview) in the layout of the first activity?not update it only on load but also have a timer for example in the second activity and every 5 seconds settext to a textview in the layout of the first activity.. any solution(either this or what i asked above about the method) would be appreciated

推荐答案

尝试使用意图的活动之间的通信

Try to use intents to communicate between activity

无论是通过onNewIntent:

either via the onNewIntent:

@Override
protected void onNewIntent(Intent intent) {
    setIntent(intent);
    handleIntent(intent);
}

private void handleIntent(Intent intent) {
    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
      String query = intent.getStringExtra(SearchManager.QUERY);
      if (query == null) {
          query = intent.getData().toString();
      }
      doMySearch(query);
    }
}

或使用监听器。

class ActivityA implements Activity {

    // Nested 'listener'
    protected class TitleBarListener extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(NEW_TITLE_INTENT))
            {
                intent.getStringExtra(NEW_TITLE_TEXT_VALUE));
            }
        }
    }

    TitleBarListener mListener;

    // then create and register
    mListener = new TitleBarListener();
    registerReceiver(mListener, new IntentFilter(NEW_TITLE_INTENT));

请务必将意图添加到android.xml

be sure to add the intents to the android.xml

    <intent-filter>
    <action android:name="android.intent.action.SEARCH"/>
    <action android:name="com.mypackage.changeTitle"/> 
</intent-filter>

,那么你应该能够播放/发送意图的活动。

then you should be able to broadcast / send intent to that activity

   Intent i = new Intent(AccountMainView.NEW_TITLE_INTENT);
   getActivity().sendBroadcast(i);


   // or if you activity is singleTop and you're using the onNewIntent:
   Intent i=new Intent(this,MainActivity.class);
   i.putExtra("methodName","Mymethod");//goes to previous Intent
   startActivity(i);//will trigger only Mymethod in MainActivity

这篇关于从其他活动的Andr​​oid调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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