In Android, I am trying to setup a callback listener for when a task is complete in a second activity. Flow is as follows:
MainActivity Starts -> Calls Second Activity -> When second activity completes call listener - > Return to Main Activity
I have done this with the following code:
//// First activity
Main Activity {
// Reference to second activity
private IntroActivity mIntroActivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
/// Listener reference
mIntroActivity = new IntroActivity();
Intent i = new Intent(MainActivity.this, IntroActivity.class);
startActivity(i);
/// Call back for second activity
mIntroActivity.setIntroListener(new IntroActivity.IntroListener() {
@Override
public void finished() {
/// When finished do something!
}
});
}
}
/// Second Activity
public class IntroActivity extends AppIntro {
/// Listener setup
public interface IntroListener {
void finished();
}
public void setIntroListener(IntroListener listener) {
this.mIntroListen = listener;
}
public IntroActivity(){
this.mIntroListen = null;
}
private IntroListener mIntroListen;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void onDonePressed(Fragment currentFragment) {
super.onDonePressed(currentFragment);
/// Call listenr from MainActivity
mIntroListen.finished();
}
}
Adding an interface listener to the second activity. When this activity is complete call finished() and return to MainActivity.
The issue I'm getting is mIntroListen.finished(); is null in the second activity.
Any ideas here would be great. I have the same interface / listener working in another project (which is another class not activity) so I'm not sure whats wrong.
Thanks in advance!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…