java 活动到活动回调侦听器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33869151/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
activity to activity callback listener
提问by Vasile Doe
Let's suppose 2 activities Activity1
and Activity2
. I need to call method methodAct1()
(inside Activity1
) from methodAct2
(inside Activity2
). I think it should work using callback listener - I don't want to use EventBus
libs!
让我们假设 2 个活动Activity1
和Activity2
。我需要调用方法methodAct1()
(内Activity1
从)methodAct2
(内部Activity2
)。我认为它应该使用回调侦听器工作 - 我不想使用EventBus
库!
I get java.lang.NullPointerException
using this code:
我开始java.lang.NullPointerException
使用这个代码:
interface:
界面:
public interface MyListener {
public void listen();
}
Activity where event is created:
创建事件的活动:
public class Activity2 extends Activity {
private MyListener myListener;
public void setUpListener(MyListener myListener) {
this.myListener = myListener;
}
private void doWork(){
//do stuff
myListener.listen();
}
}
Activity where I'd like to get that event when work is done:
我希望在工作完成后获得该事件的活动:
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
}
});
}
}
回答by Knossos
This is absolutely not possible. You never instanciate a new Activity yourself. You will not have two Activities running at the same time.
这是绝对不可能的。你永远不会自己实例化一个新的 Activity。您不会同时运行两个活动。
If you want another Activity to do something, based on what your previous Activity wants, then you need to add that to your Intent.
如果您希望另一个 Activity 根据您之前的 Activity 想要做某事,那么您需要将其添加到您的 Intent 中。
Intent intent = new Intent(this, Activity2.class);
intent.putExtra("data field", "data value");
startActivity(intent);
If you want specific functionality through a callback then you might be thinking of Fragments. In this way, you can have the same Activity running and it can tell individual Fragments what they need to do.
如果您希望通过回调实现特定功能,那么您可能会考虑Fragments。通过这种方式,您可以运行相同的 Activity,并且它可以告诉各个 Fragment 他们需要做什么。
回答by ΦXoc? ? Пepeúpa ツ
The NPE is happening because of your statement:
NPE 的发生是因为您的声明:
Activity2 activity2 = new Activity2(); <--
you should never do this, and instead you should do in the Activity 1:
你永远不应该这样做,而应该在活动 1 中这样做:
Intent intent = new Intent(this, Activity2.class);
intent.putExtra("dataKey", "dataValue");
startActivityForResult(pickContactIntent, CALLBACK_REQUEST);
the startActivityForResult()offers a callback from Activity 2 to Activity 1, and you have to override the result in activity 1:
该startActivityForResult()提供自Activity 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)
}
}
}
回答by Tushar Pramanik
You could do that in your approach like this....
你可以像这样用你的方法做到这一点......
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));
}
}
Though it's not best approach and in order to work with this mechanism your activity1 needs to be created.
尽管这不是最佳方法,但为了使用此机制,您需要创建活动 1。
回答by tdjprog
You can do it like this > - STEP 01: Implement a shared interface
你可以这样做 > - STEP 01: 实现一个共享接口
public interface SharedCallback {
public String getSharedText(/*you can define arguments here*/);
}
- STEP 02: Implement a shared class
- STEP 02:实现共享类
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;
}
}
- STEP 03 :: Play with code in First Activity
- 步骤 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";
}
}
- STEP 04 :: Finalize the game in SecondActivity
- STEP 04 :: 在 SecondActivity 中完成游戏
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();
}
}
you can also implement a backword callback (from First to Second) to get some results from SecondAvtivity or call some methods
您还可以实现反向回调(从 First 到 Second)以从 SecondAvtivity 获取一些结果或调用一些方法