I have a method in my application which starts a new Activity which starts the Camera Application. The Camera Application returns a result (picture taken or not taken), and I store this result in the Intent-Bundle.
In the original method I want to read this bundle, which does not work because the activity is not finished yet.
How can I wait for the activity to finish before going on in my method?
Method
#Override
public boolean startChallenge(Context context) {
Intent cam = new Intent(context, CameraIntent.class);
cam.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(cam);
Boolean done = cam.getExtras().get("done"); // << this fails obviously
}
Activity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i, CAPTURE_IMAGE_CAPTURE_CODE);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
intent = getIntent();
if (requestCode == CAPTURE_IMAGE_CAPTURE_CODE) {
if (resultCode == RESULT_OK) {
intent.putExtra("done", true);
} else if (resultCode == RESULT_CANCELED) {
intent.putExtra("done", false);
}
}
finish();
}
I would like to keep the structure of the code because the startChallenge() Method is inherited from a superclass (Challenge) and startChallenge is different from type to type (e.g. there is a challenge where you have to take a picture, another challenge where you have to answer something, and so on). The method gets called in another activity, depending on the type of challenge.
startChallenge should have a reference to the Activity that expects the result back to be able to call startActivityForResult
Method
#Override
public boolean startChallenge(Activity activityB) {// Activity B in which you are expecting this result back.
Intent cam = new Intent(activityB, ActivityA.class);
activityB.startActivityForResult(cam, REQUEST_CODE_OPEN_CAMERA);
}
Activity A
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
intent = getIntent();
if (requestCode == CAPTURE_IMAGE_CAPTURE_CODE) {
if (resultCode == RESULT_OK) {
intent.putExtra("done", true);
} else if (resultCode == RESULT_CANCELED) {
intent.putExtra("done", false);
}
}
setResult(RESULT_OK, intent);
finish();
}
Another approach would be to use LocalBroadcastManager or Otto
Activity A
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAPTURE_IMAGE_CAPTURE_CODE) {
if (resultCode == RESULT_OK) {
BusProvider.getInstance().post(new ImageCapturedEvent(true));
} else if (resultCode == RESULT_CANCELED) {
BusProvider.getInstance().post(new ImageCapturedEvent(false));
}
}
finish();
}
Related
After updating my app to AndroidX I noticed startActivityForResult() is depreciated. I looked through the documentation and found some good explanations, but I'm still confused as to how to handle request codes. I've tried adding the request code param to onActivityResult, but obviously that does not work. This is my old onActivityResult.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE_1) {
fetchTags();
} else if (requestCode == REQUEST_CODE_2) {
if (resultCode == RESULT_CANCELED) {
finish();
}
}
}
Do I need to create separate ActivityResultLaunchers for both request codes?
There is no need of Request Codes in the new API. You launch individual intents and you get results under their own scopes.
Old Way
public void openSomeActivityForResult() {
Intent intent = new Intent(this, SomeActivity.class);
startActivityForResult(intent, 123);
}
#Override
protected void onActivityResult (int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK && requestCode == 123) {
doSomeOperations();
}
}
New Way
// You can do the assignment inside onAttach or onCreate, i.e, before the activity is displayed
ActivityResultLauncher<Intent> someActivityResultLauncher = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
new ActivityResultCallback<ActivityResult>() {
#Override
public void onActivityResult(ActivityResult result) {
if (result.getResultCode() == Activity.RESULT_OK) {
// There are no request codes
Intent data = result.getData();
doSomeOperations();
}
}
});
public void openSomeActivityForResult() {
Intent intent = new Intent(this, SomeActivity.class);
someActivityResultLauncher.launch(intent);
}
You can find more info in the documentation
https://developer.android.com/training/basics/intents/result
I am starting an activity with startActivityForResult like this
#Override
public void onEmptyViewClicked(Calendar time) {
Bundle bundle = new Bundle();
bundle.putSerializable("time",time);
bundle.putInt("fragmentId",1);
Intent intent = new Intent(this,AddEventActivity.class);
intent.putExtras(bundle);
startActivityForResult(intent,1);
}
and in AddEventActivity i add fragments depending on the fragment id.The first fragment add events and second fragment delete events.onAddEvent and onDeleteEvent i setResult with appropriate data like this
#Override
public void onAddEventClicked(Bundle bundle) {
Intent intent = new Intent(this,MainActivity.class);
intent.putExtras(bundle);
setResult(1,intent);
finish();
}
#Override
public void eventDelete(Events event) {
Bundle bundle = new Bundle();
bundle.putSerializable("deleteEvent",event);
Intent newIntent = new Intent(this,MainActivity.class);
newIntent.putExtras(bundle);
setResult(2,newIntent);
finish();
}
OnActivityResult code is something like this
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode == 1 && data != null){
if(requestCode == 1){
}
}else if(resultCode == 2){
}
}
}
Now onActivityResult is getting called onAdding event but not on deleting event.eventDelete method is getting called but it is not triggering onActivityResult.What could be the problem?Thankyou.
UPDATED
I put a debug at first line in onActivityResult but debugger doesn't stop there.
startActivityForResult(intent,1);
here 1 is your requestCode
setResult(1,intent);
here 1 is your resultCode
you are setting same resultcode for Two events
change your onActivityResult to
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == 1) { //add event
if (requestCode == 1) {
if (data != null) {
if (data.getExtras().containsKey("deleteEvent")) {
} else {
}
}
}
} else if (resultCode == 2) { //delete event
//write code
}
}
you should add RESULT_OK and validate this resultCode:
Intent returnIntent = new Intent();
returnIntent.putExtra("returndata");
setResult(Activity.RESULT_OK, returnIntent);
finish();
after this, u need add the validation in your onActivityResult method...
if(requestCode == CODE_REQUEST){
if(resultCode == Activity.RESULT_OK){
//do something here
}{
//catch cancel result
}
}
I know there are several questions about this, but I don't found a solution for my problem.
I have ActivityA which extends AppCompatActivity. It starts an ActivityB
Activity A
Intent intent = new Intent(this, ActivityB.class);
intent.putExtra("data", data);
startActivityForResult(intent, 1);
....
#Override
protected void onActivityResult(int requestCode, int result, Intent intent) {
super.onActivityResult(requestCode, result, intent);
if (requestCode != 1) { // check code
return;
}
if (intent == null) { // HERE INTENT IS NULL
return;
}
}
Activity B
// code called when an asynctask is done
Intent i = new Intent();
i.putExtra("dataone", "test");
i.putExtra("datatwo", objet);
setResult(RESULT_OK, i);
finish();
I don't understand why intent is null in onActivityResult() method.
Two things. I would refactor your code like the following:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 1) {
if (resultCode == RESULT_OK) {
// example
// String myString = intent.getString("myStringFrom2ndActivity")
}
}
}
and also make sure that you are calling the right RESULT_OK. It should be something like Activity.RESULT_OK.
I have the next activities:
Activity1
//declare
private static final int SAVE_DATA_FROM_ACTIVITY = 203;
//........... not important code
//button to open second Activity
public void btn_openSecondActivity(View view)
{
Intent intent = new Intent(Activity2.this, Activity1.class);
startActivityForResult(intent, SAVE_DATA_FROM_ACTIVITY);
}
}
protected void onActivityResult (int requestCode, int resultCode, Intent data)
{
if (requestCode == SAVE_DATA_FROM_activity)
{
name= data.getStringExtra("Name");
}
}
//....... not important code
Activity2
On Second Activity I have two buttons:
Cancel
Save
//............
//declare
private static final int OK_RESULT_CODE = 1;
//Cancel button
public void btn_cancel(View view)
{
finish();
}
//Save button
public void btn_save (View view)
{
Intent intent = new Intent();
intent.putExtra("Name",et_name.getText().toString());
setResult(OK_RESULT_CODE, intent);
finish();
}
PROBLEM
When I click Save button all works perfect, but the problem it's when I click Cancel button, then it's reports an error:
Failure delivering result ResultInfo{who=null, request=203, result=0, data=null} to activity {com.example.alvaro.project/com.alvaro.project.Activity1}: java.lang.NullPointerException
I understand the problem, when I cancel is not the same result code but i don't know how I can solve it
Any suggestions?
You have issue in onActivityResult method. You don't check result.
Change your condition from :
if (requestCode == SAVE_DATA_FROM_activity)
to:
if (resultCode == OK_RESULT_CODE && requestCode == SAVE_DATA_FROM_activity)
Change this
if (requestCode == SAVE_DATA_FROM_activity)
{
name= data.getStringExtra("Name");
}
into
if (requestCode == SAVE_DATA_FROM_activity&&resultCode==RESULT_OK)
{
name= data.getStringExtra("Name");
}
and
Your cancel method is like
public void btn_cancel(View view)
{
setResult(RESULT_CANCELED);
finish();
}
And instead of OK_RESULT_CODE use Android default ok like Activity.RESULT_OK
Check if you manage to set the result
protected void onActivityResult (int requestCode, int resultCode, Intent data)
{
if (requestCode == SAVE_DATA_FROM_activity && resultCode = Activity2.OK_RESULT_CODE)
{
name= data.getStringExtra("Name");
} else {
//probably btn_cancel pressed
}
}
check resultCode in onActivityResult
protected void onActivityResult (int requestCode, int resultCode, Intent data)
{
if (resultCode == 1/*OK_RESULT_CODE from Second Activity */ && requestCode == SAVE_DATA_FROM_activity)
{
name= data.getStringExtra("Name");
}
}
Activity1
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == SAVE_DATA_FROM_activity) {
if (resultCode == Activity.RESULT_OK) {
name = data.getStringExtra("Name");
} else if (resultCode == Activity.RESULT_CANCELED){
// TODO something
}
}
}
Activity2
delete field OK_RESULT_CODE
//Cancel button
public void btn_cancel(View view) {
setResult(Activity.RESULT_CANCELED, new Intent());
finish();
}
//Save button
public void btn_save(View view) {
Intent intent = new Intent();
intent.putExtra("Name", et_name.getText().toString());
setResult(Activity.RESULT_OK, intent);
finish();
}
Why my toast message is showing? My CEVAP_SORGULA variable equals 322, but my intents request code is 332
private final static int CEVAP_SORGULA = 322;
public void degistirActivity(final View view){
startActivityForResult(new Intent(this,veriTopla.class),332);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if (resultCode == Activity.RESULT_OK && requestCode == CEVAP_SORGULA ){
Toast.makeText(this, "Cevabin: " + data.getExtras().getInt("Cevap"),Toast.LENGTH_LONG).show();
}
super.onActivityResult(requestCode, resultCode, data);
}
I see clearly a type error between the CEVAP_SORGULA and the value passed via startActivityForResult. To fix it provide the variable instead of the hardcoded WRONG value.
startActivityForResult(new Intent(this,veriTopla.class),CEVAP_SORGULA);
you can try
Intent i = new Intent(this, SecondActivity.class);
startActivityForResult(i, 332);
DEFAULT METHOD
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Check which request we're responding to
if (requestCode == PICK_CONTACT_REQUEST) {
// Make sure the request was successful
if (resultCode == RESULT_OK) {
// The user picked a contact.
// The Intent's data Uri identifies which contact was selected.
// Do something with the contact here (bigger example below)
}
}
}
SEE THIS EXAMPLE FROM ANDROID DEVELOPER SITE, MAKE YOU HELP
public class MyActivity extends Activity {
...
static final int PICK_CONTACT_REQUEST = 0;
protected boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
// When the user center presses, let them pick a contact.
startActivityForResult(
new Intent(Intent.ACTION_PICK,
new Uri("content://contacts")),
PICK_CONTACT_REQUEST);
return true;
}
return false;
}
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
if (requestCode == PICK_CONTACT_REQUEST) {
if (resultCode == RESULT_OK) {
// A contact was picked. Here we will just display it
// to the user.
startActivity(new Intent(Intent.ACTION_VIEW, data));
}
}
}
}