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
}
}
Related
I have problem with onActivityResult, whatever I'm doing I can't get resultCode right.
I know that there are similar questions but at the end they didn't help me and I couldn't fix it
MainActivity: method which will open new Activity Popup.class
public void openShopView(){
Intent intent = new Intent(this, Popup.class);
Bundle b = new Bundle();
b.putString("which", "ShopMain");
intent.putExtras(b);
startActivityForResult(intent, 1);
}
Second Activity: method which will open yet another Activity Popup.class just with different layout
shop_c1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getIntent());
Bundle b = new Bundle();
b.putString("which", "ShopBuildings");
intent.putExtras(b);
startActivity(intent);
finish();
}
});
Third Activity: and there is method which should setResult and close Activity
building2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.i("LOG_NEW: ", "" + getCurrentBuildingTable(1) + ", " + checkSlotTable(1));
if(getCurrentBuildingTable(1) && checkSlotTable(1) == -1) {
Intent returnIntent = getIntent();
returnIntent.putExtra("result", 1);
setResult(RESULT_OK, returnIntent);
finish();
}else if (checkSlotTable(1) == -1){
Log.i("LOG_NEW: ", "Building already exist");
}
else{
Log.i("LOG_NEW: ", "Not enough resources");
}
}
});
At the end there is onActivityResult() from MainActivity
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.i("LOG_RES: ", "Checking.. " + requestCode + ", " + resultCode);
if (requestCode == 1) {
if(resultCode == RESULT_OK){
String result = data.getStringExtra("result");
Log.i("LOG_RES: ", result);
}
}
}
Whatever I'm doing I can't start if(resultCode == RESULT_OK) loop and resultCode is always 0..
Thanks for help
setResult must be called in Second Activity, since intent of second activity was passed in startActivityForResult.
However, you can delegate the result code of Third Activity to Second Activity, then to third.
Change your Second Activity to something like this:
shop_c1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getIntent());
Bundle b = new Bundle();
b.putString("which", "ShopBuildings");
intent.putExtras(b);
startActivityForResult(intent,1);
//Remove finish from here
}
});
then also add this in Second Activity
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==1){
setResult(resultCode,data);
}
finish();
}
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();
}
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();
}
I use startActivityForResult for Activity1 to start Activity2 :
btnSelectFiles.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
savePreferences();
Intent i = new Intent(getApplicationContext(),
FileManagerActivity.class);
Bundle mBundle = new Bundle();
mBundle.putString("FileManager", "NewOrder");
i.putExtras(mBundle);
startActivityForResult(i, Constants.addFilesCode);
}
});
onActivityResult method :
and in Activity2 :
Intent returnIntent = new Intent();
setResult(1,returnIntent);
FileManagerActivity.this.finish();
But in the Activity1
requestCode is correct, but the resultCode is always 0.
I do not use Back buttons.
my onActivityResult in the Activity1
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.i(TAG, "onActivityResult");
Log.i(TAG,
"onActivityResult requestCode" + Integer.toString(requestCode)
+ "resultCode" + Integer.toString(resultCode));
// adding files to the list if the files were added successfully
if (requestCode == Constants.addFilesCode)
{
// successfull operation
if (resultCode == 1)
{
if (adapter == null)
addFiles();
else if (adapter.getCount() == 0)
addFiles();
else {
adapter.notifyDataSetChanged();
changeFileHeader();
}
btnFilesRemove.setVisibility(View.VISIBLE);
for (int b = 0; b < FileManagerActivity.getFinalAttachFiles()
.size(); b++) {
checks.add(b, 0);
}
}
}
My issue was that I was starting activity with FLAG_ACTIVITY_NO_HISTORY. As soon as I removed it, resultCode started propagating back to caller.