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!
The issue I'm getting is mIntroListen.finished(); is null in the second activity.
You will always get null
because mIntroActivity
instance and your actual running instance of IntroActivity
are different.
mIntroActivity = new IntroActivity();
Creating activity this way is not a good idea. To start a Activity
you should always use startActivity()
.
If you want to get back to MainActivity
with some data from IntroActivity
or just back when completes call listener you should use startActivityForResult(intent)
in MainActivity
. This method will give you options to set result you have found in IntroActivity
and you can use those result in MainActivity
by onActivityResult()
method.
For your case use startActivityForResult(intent)
to start your IntroActivity
and when completes call listener just finish IntroActivity
then onActivityResult()
method will be called in MainActivity
. And do what you want in onActivityResult()
Check this official link Getting a Result from an Activity and you can also check this answer
If you study the below part of your code carefully, you will see that when you create an Intent
you are passing IntroActivity.class
. This means that you are just passing a Class
reference, and not an instantiated object of that class. So, when the activity is launched it has no reference to your instantiated mIntroActivity
. So, obviously introListener
will also be null
. This is an expected behavior.
/// 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!
}
});
For getting result back from a launched activity, you have to use startActivityForResult(Intent intent)
. Please check the documentation for more information.
The modified code will be something like below.
MainActivity
private static final int INTRO_ACTIVITY_REQUEST_CODE = 771;
@Override
protected void onCreate(Bundle savedInstanceState) {
Intent i = new Intent(MainActivity.this, IntroActivity.class);
startActivityForResult(i, INTRO_ACTIVITY_REQUEST_CODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == INTRO_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
String resultValue = data.getStringExtra("KEY");
}
}
}
IntroActivity
@Override
public void onDonePressed(Fragment currentFragment) {
super.onDonePressed(currentFragment);
Intent resultIntent = new Intent();
resultIntent.putExtra("KEY", "VALUE");
setResult(RESULT_OK, resultIntent); // RESULT_OK means success
finish();
}
Check the docs for startActivityForResult(Intent, Key)
You are supposed to use that for getting results back from another activity.
Instantiating the Activity with new()
is wrong.
You didn't actually initialise the second activity just by calling new on it, not even a good idea.
I am sure there is better way to do your job using handler like as follow:
Declare a handler in MainActivity
and instantiate it like below as simple:
private Handler = new Handler(){
public void handleMessage(Message message){
// Do your jobs
}}
Ship the handler with your intent to the second activity using Messenger
:
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("key_handler", new Messenger(handler));
startActivity(intent);`
Get the intent in SecondActivity.class
, and extract the messenger from it, when your second activity has done its job, just call messenger.send(message)
to send message back to the main activity.
Intent intent = getIntent();
Messenger messenger = intent.getExtras("key_handler");
// Do jobs .....
// When done, call send method on messenger, maybe you want sth to sent back to main activity, just use bundle.
Message message = Message.obtainMessage();
Bundle bundle = new Bundle();
// Put sth in the bundle, then call setData()
message.setData(bundle);
messenger.send(message)`
Back to handleMessage(Message message)
to grab your message.
Note that somethings above are ignored just for a simpler answer, you gotta do some search on your own for right usage.