我们假设有两项活动Activity1
和Activity2
。我需要从methodAct1()
(内部Activity1
)调用方法methodAct2
(在Activity2
内)。我认为它应该使用回调监听器 - 我不想使用EventBus
库!
我使用此代码获取java.lang.NullPointerException
:
接口:
public interface MyListener {
public void listen();
}
创建事件的活动:
public class Activity2 extends Activity {
private MyListener myListener;
public void setUpListener(MyListener myListener) {
this.myListener = myListener;
}
private void doWork(){
//do stuff
myListener.listen();
}
}
我希望在工作完成后获得该活动的活动:
public class Activity1 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Activity2 activity2 = new Activity2();
activity2.setUpListener(new setUpListener() {
@Override
public void listen() {
// get the event here
}
});
}
}
答案 0 :(得分:3)
由于您的声明,NPE正在发生:
Activity2 activity2 = new Activity2(); <--
你永远不应该这样做,而应该在活动1中进行:
Intent intent = new Intent(this, Activity2.class);
intent.putExtra("dataKey", "dataValue");
startActivityForResult(pickContactIntent, CALLBACK_REQUEST);
startActivityForResult()提供从活动2到活动1的回调,您必须覆盖活动1中的结果:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Check which request we're responding to
if (requestCode == CALLBACK_REQUEST) {
// Make sure the request was successful
if (resultCode == RESULT_OK) {
// The Intent's data Uri identifies which contact was selected.
// Do something with the contact here (bigger example below)
}
}
}
答案 1 :(得分:3)
绝对不可能。你永远不会自己实现一个新的活动。您不会同时运行两个活动。
如果您希望其他Activity根据您之前的Activity想要做什么,那么您需要将其添加到您的Intent。
Intent intent = new Intent(this, Activity2.class);
intent.putExtra("data field", "data value");
startActivity(intent);
如果您想通过回调获得特定功能,那么您可能会考虑Fragments。通过这种方式,您可以运行相同的Activity,它可以告诉单个Fragments他们需要做什么。
答案 2 :(得分:1)
您可以按照这种方式进行操作。...
public class Activity2 extends AppCompatActivity {
private static MyListener myListener;
public static void setUpListener(MyListener Listener) {
myListener = Listener;
}
public void doWork(View view) {
myListener.listen();
}
}
public class Activity1 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Activity2.setUpListener(new MyListener() {
@Override
public void listen() {
Log.d("Hello", "Hello World");
}
});
}
public void goToAnotherActivity(View view) {
startActivity(new Intent(Activity1.this, Activity2.class));
}
}
尽管这不是最佳方法,但要使用此机制,需要创建activity1。
答案 3 :(得分:0)
您可以这样做> -步骤01:实现共享界面
public interface SharedCallback {
public String getSharedText(/*you can define arguments here*/);
}
final class SharedMethode {
private static Context mContext;
private static SharedMethode sharedMethode = new SharedMethode();
private SharedMethode() {
super();
}
public static SharedMethode getInstance() {
return sharedMethode;
}
public void setContext(Context context) {
if (mContext != null)
return;
mContext = context;
}
public boolean contextAssigned() {
return mContext != null;
}
public Context getContext() {
return mContext;
}
public void freeContext() {
mContext = null;
}
}
-步骤03 ::在“首次活动”中玩代码
public class FirstActivity extends Activity implements SharedCallback {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
// call playMe from here or there
playMe();
}
private void playMe() {
SharedMethode.getInstance().setContext(this);
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);
}
@Override
public String getSharedText(/*passed arguments*/) {
return "your result";
}
}
public class SecondActivity extends Activity {
private SharedCallback sharedCallback;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
if (SharedMethode.getInstance().contextAssigned()) {
if (SharedMethode.getInstance().getContext() instanceof SharedCallback)
sharedCallback = (SharedCallback) SharedMethode.getInstance().getContext();
// to prevent memory leak. no further needs
SharedMethode.freeContext();
}
// You can now call your implemented methodes from anywhere at any time
if (sharedCallback != null)
Log.d("TAG", "Callback result = " + sharedCallback.getSharedText());
}
@Override
protected void onDestroy() {
sharedCallback = null;
super.onDestroy();
}
}
您还可以实现backword回调(从First到Second)以从SecondAvtivity中获得一些结果或调用某些方法