简体   繁体   English

来自其他活动的Android调用方法

[英]Android call method from an other activity

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). 我已经读了一堆帖子,文章和所有内容,但是我无法解决。.我制作了一个android应用程序..我开始了第一个活动(头等舱)。 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")

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..... 当我运行此程序时,应用程序崩溃..我尝试了try catch exeption,然后打印到exeption,这是null的一个错误...我无法使它正常工作.....

thanks in advance! 提前致谢!

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 PS有什么方法可以启动第二个活动并更新第一个活动的布局中的某些值(在文本视图中)?不仅可以在加载时更新它,还可以在第二个活动中有一个计时器,例如每隔5秒钟将settext设置为在第一个活动的布局中的文本视图..任何解决方案(无论是这个还是我在上面关于方法的问题)

Try to use intents to communicate between activity 尝试使用意图在活动之间进行沟通

either via the onNewIntent: 通过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);
    }
}

or use a listener. 或使用侦听器。

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

be sure to add the intents to the android.xml 确保将意图添加到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

I think that your approach is not good. 我认为您的方法不好。 what kind of method do you want to run in the first Activity ? 您要在第一个Activity运行哪种方法? and what is the point in doing that if your Activity is not in use by the user at the moment? 如果用户当前未使用您的“ Activity ,这样做有什么意义?

what you should do is to define your actions as part of your onResume() method of the first Activity and place their the method/s you want to run, following that just pas a Bundle or an Extra from you second Activity to the first one to determine what action should be taken. 您应该做的是将操作定义为第一个Activity onResume()方法的一部分,并放置您要运行的方法,然后将第二个ActivityBundleExtra Activity到第一个Activity确定应采取的措施。

Create an Interface, implement that interface in the first activity, and then call the instanciated methods from the second activity. 创建一个接口,在第一个活动中实现该接口,然后从第二个活动中调用实例化的方法。

You can pass the instantiated interface object in the intent when starting the second activity. 启动第二个活动时,可以按意图传递实例化的接口对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM