Reputation: 331
I am working on an Android library which starts it's own Activity. I need to have library's method working synchronously, it means I want to wait until the started Activity is done and then return a value. How can I do it? I tried different approaches and nothing seems to do what I want.
This is the librairy's method starting an activity:
@Override
public Response admin(TypeEnumeration type) {
Intent i = new Intent(context, NewActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra("type", type.value());
context.startActivity(i);
Response response = NewActivity.getResponse();
return response;
}
The activity starts many different fragments and waits from user's interaction.
I don't want to use active waiting for NewActivity.getResponse()
not to be null. I tried adding an extra object to use wait()
/notyfy()
on it but then the activity was not starting at all.
Any tips will be appreciated! Thanks
Upvotes: 1
Views: 1695
Reputation: 141
I had implemented the same sort of thing a few days back. Just follow the below provided step for the same:
Use the application reference (can get using activity.getApplication()) and register for activity lifecycle callbacks using:
application.registerActivityLifecycleCallbacks(ActivityLifecycleCallbacks
callback)
Now open the activity as you were doing earlier.
context.startActivity(i);
Store the data that you want to pass from opened activity; in some variable that will stay after activity got killed. It can be some static reference in your library, some member variable in the object that has a lifetime more than the activity or persistence storage like shared preferences.
Now close your activity using activity.finish()
.
At this time your previous activity will get resume and you will get a call in activity lifecycle method i.e onActivityResumed(Activity activity)
Enjoy!
Upvotes: 0
Reputation: 11
Intent intent = new Intent(context, YourNewActivity.class);
((Activity) context).startActivityForResult(intent, ResponseCode);
Upvotes: 0
Reputation: 133
The most common scenario (which is what yours sounds like) is when a child Activity is used to get user input - such as choosing a contact from a list or entering data in a dialog box. In this case you should use startActivityForResult to launch your child Activity.
This provides a pipeline for sending data back to the main Activity using setResult. The setResult method takes an int result value and an Intent that is passed back to the calling Activity.
Intent resultIntent = new Intent();
// TODO Add extras or a data URI to this intent as appropriate.
resultIntent.putExtra("some_key", "String data");
setResult(Activity.RESULT_OK, resultIntent);
finish();
To access the returned data in the calling Activity override onActivityResult. The requestCode corresponds to the integer passed in in the startActivityForResult call, while the resultCode and data Intent are returned from the child Activity.
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode) {
case (MY_CHILD_ACTIVITY) : {
if (resultCode == Activity.RESULT_OK) {
// TODO Extract the data returned from the child Activity.
String returnValue = data.getStringExtra("some_key");
}
break;
}
}
}
Upvotes: 1