我想调用其他活动的方法。我搜索了很多,自己尝试但失败了。当我从类到类调用时,它就像classA m = new classA(); m.function(); 但我想从另一种活动方法打电话。请帮忙
public void onNotificationPosted(StatusBarNotification sbn) {
ChatActivity ch=new ChatActivity();
ch.location();
}
答案 0 :(得分:1)
public class ApplicationContext extends Application {
public ActivityMain activityMain;
public void setActivityMain(ActivityMain activityMain) {
this.activityMain = activityMain;
}
public ActivityMain getActivityMain() {
return activityMain;
}
在ActivityMain.class
中public class ActivityMain extends BaseProject implements MainView {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ApplicationContext.getInstance().setActivityMain(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
ApplicationContext.getInstance().setActivityMain(null);
}
在活动,片段,广播接收器或服务等任何地方使用它,如
if (ApplicationContext.getInstance().getActivityMain()!=null){
ApplicationContext.getInstance().getActivityMain().callAnyMethod();
}
享受:)