setResult does not work on #EActivity - java

I am trying to setResult on my "Register" Button, but nothing happens:
SignInActivity:
Start activity for result:
Intent intent = RegisterActivity_.intent(this).get();
this.startActivityForResult(intent, REQUEST_CODE_USER_REGISTER);
On Activity Result
#OnActivityResult(REQUEST_CODE_USER_REGISTER)
protected void onResult(int resultCode) {
if (resultCode == RESULT_OK)
DashboardActivity_.intent(this).start();
else if (resultCode == RESULT_CANCELED) {
this.showDialogAlert("Unexpected error", null);
}
RegisterActivity:
this.setResult(RESULT_OK);
I am using AndroidAnnotations and I'm not using the this.finish() method because it set the property android:noHistory="true" in all Activities on AndroidManifest.xml. I am also setting the parent of each activity there. Have tried to remove these settings and setResult continued without work. Has anyone experienced this?

I am using AndroidAnnotations and I'm not using the this.finish() method because it set the property android:noHistory="true"in all Activities on AndroidManifest.xml.
I think your issue is here. When you're using startActivityForRestult, the expected workflow is to launch a new activity, do some work and then close this activity to go back to the previous one with the result of the work. So, you can't use android:noHistory="true" here because it doesn't make any sense.
Also, if you look at Activity's source code, you'll see that result is propagated from the finish method.

Start Activity for Result. your code is Ok
Intent intent = RegisterActivity_.intent(this).get();
this.startActivityForResult(intent, REQUEST_CODE_USER_REGISTER);
Now Inside the Activity that you start for the result set the result status like below code.
setResult(resultCode); result status possible values are like RESULT_CANCELED,RESULT_OK.etc
Activity that starts the new activity for result must override the below method and this method work when the activity that started for result finish.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
}

I think the method is..
onActivityResult(int requestCode, int resultCode, Intent data)
not onResult

Related

StartActivityForResult go back

I am using StartActivityForResult for multiple activities. My main activity is where I initialize it. On my second activity I input some values and pass to a third activity. Now, When i'm on the third activity I want to be able to go back to the second activity if ever I want to edit the values i passed. What should I do?
MainAct.java
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE)
{
if (resultCode == Activity.RESULT_OK)
{
//Something
}
}
SecondAct.java
Intent vd2 = new Intent(ViolatorDetails1.this,ViolatorDetails2.class);
vd2.putExtra("name",name);
vd2.putExtra("lname",lname);
vd2.putExtra("lnumber",lnumber);
vd2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
vd2.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
startActivity(vd2);
finish();
ThirdAct.java
Intent intent = new Intent();
intent.putExtra("firstname",name);
intent.putExtra("lastname", lname);
intent.putExtra("licensenumber", lnumber);
setResult(Activity.RESULT_OK, intent);
finish();
How can I go back to second Activity from third activity to edit some values if ever?
You should not call finish() on second activity when starting the third one.
Then onActivityResult() will be call when third activity is finished.
Call
startActivityForResult(vd2);
instead of
startActivity(vd2);
simply remove finish();
Intent vd2 = new Intent(ViolatorDetails1.this,ViolatorDetails2.class);
vd2.putExtra("name",name);
vd2.putExtra("lname",lname);
vd2.putExtra("lnumber",lnumber);
vd2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
vd2.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
startActivity(vd2);
finish(); //remove this line
in this way when your third activity closes user will go back to 2nd one, also you should implement onActivityResult in your 2nd activity so you can handle weather user wants to edit or has finished and should go back to 1st activity! (i.e. setting the result of the intent came from 1st activity and finish the 2nd one!)
here is what I mean in code:
In your 2nd activity do this,
#override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE)
{
if (resultCode == Activity.RESULT_OK)
{
// user should have done his job on 3rd activity and we should finish the 2nd activity to go back to first activity!
}else{
//user still editing!
}
}
and instead of startActivity(vd2); do startActivityForResult(vd2);

Can I pass an Intent extra on finish()?

I'm wondering, is it possible to send information to the activity that I return to after calling finish()?
For example, I have an Activity SendMessageActivity.class which allows the user to post a message to their feed. Once that message has been saved to the server, I call finish(). Should I instead just start my MainActivity.class with a new Intent? Or is it better for life cycle development to just finish SendMessageActivity.class?
I don't see the point of starting a new activity since closing the current one will always bring you back to MainActivity.class. How can I just send a String extra after finishing the current Activity?
Use onActivityResult.
This might help you to understand onActivityResult.
By using startActivityForResult(Intent intent, int requestCode) you can start another Activity and then receive a result from that Activity in the onActivityResult() method.So onActivityResult() is from where you start the another Activity.
onActivityResult(int requestCode, int resultCode, Intent data) check the params here. request code is there to filter from where you got the result. so you can identify different data using their requestCodes!
Example
public class MainActivity extends Activity {
// Use a unique request code for each use case
private static final int REQUEST_CODE_EXAMPLE = 0x9988;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create an Intent to start AnotherActivity
final Intent intent = new Intent(this, AnotherActivity.class);
// Start AnotherActivity with the request code
startActivityForResult(intent, REQUEST_CODE_EXAMPLE);
}
//-------- When a result is returned from another Activity onActivityResult is called.--------- //
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// First we need to check if the requestCode matches the one we used.
if(requestCode == REQUEST_CODE_EXAMPLE) {
// The resultCode is set by the AnotherActivity
// By convention RESULT_OK means that what ever
// AnotherActivity did was successful
if(resultCode == Activity.RESULT_OK) {
// Get the result from the returned Intent
final String result = data.getStringExtra(AnotherActivity.EXTRA_DATA);
// Use the data - in this case, display it in a Toast.
Toast.makeText(this, "Result: " + result, Toast.LENGTH_LONG).show();
} else {
// AnotherActivity was not successful. No data to retrieve.
}
}
}
}
AnotherActivity <- This the the one we use to send data to MainActivity
public class AnotherActivity extends Activity {
// Constant used to identify data sent between Activities.
public static final String EXTRA_DATA = "EXTRA_DATA";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_another);
final View button = findViewById(R.id.button);
// When this button is clicked we want to return a result
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Create a new Intent as container for the result
final Intent data = new Intent();
// Add the required data to be returned to the MainActivity
data.putExtra(EXTRA_DATA, "Some interesting data!");
// Set the resultCode to Activity.RESULT_OK to
// indicate a success and attach the Intent
// which contains our result data
setResult(Activity.RESULT_OK, data);
// With finish() we close the AnotherActivity to
// return to MainActivity
finish();
}
});
}
#Override
public void onBackPressed() {
// When the user hits the back button set the resultCode
// to Activity.RESULT_CANCELED to indicate a failure
setResult(Activity.RESULT_CANCELED);
super.onBackPressed();
}
}
Note : Now check in MainActivity you startActivityForResult there you specify a REQUEST_CODE. Let's say you want to call three different Activities to get results.. so there are three startActivityForResult calls with three different REQUEST_CODE's. REQUEST_CODE is nothing but a unique key you specify in your activity to uniquely identify your startActivityForResult calls.
Once you receive data from those Activities you can check what is the REQUEST_CODE, then you know ah ha this result is from this Activity.
It's like you send mails to your lovers with a colorful covers and ask them to reply in the same covers. Then if you get a letter back from them, you know who sent that one for you. awww ;)
You can set result of an activity, which allow you to data into an initent.
In your first activity, call the new one with startActivityForResult() and retrieve data in method onActivityResult. Everything is in documentation.
try this:
in First Activity:
Intent first = new Intent(ActivityA,this, ActivityB.class);
startActivityForResult(first, 1);
Now in Second activity: set Result during finish()
Intent intent = new Intent();
intent.putExtra("result",result); //pass intent extra here
setResult(RESULT_OK,intent);
finish();
First activity Catch the result;
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
// check if the request code is same as what is passed here it is 1
if(requestCode==1)
{
String message=data.getStringExtra("result");
//get the result
}
}
If you call finish() to avoid that the user go back to SendMessageActivity.class, you can set this flags to your intent:
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
This will open the MainActivity and remove the SendMessageActivity from the activities stack.

StartActivityForResult, but activity finishes

I am trying to use Adobe Image Edit SDK to edit a photo then redirect to another activity, from my custom camera activity.
This works from another activity, simply by creating the Image edit Intent, using startActivityForResult, then treating the "Done" callback in said activity, in the method onActivityResult.
Intent imageEditorIntent = new AdobeImageIntent.Builder(mContext)
.setData(selectedImageUri)
.withToolList(tools)
.withOutput(new File(mLastSavedFilePath))
.build();
startActivityForResult(imageEditorIntent, 2);
and then
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == 2) { // i get here
However, when I do this from my Custom Camera Activity, the activity ends when I click "done" in the image edit sdk (its onDestroy is called) before it gets to the result
Intent intent = FileUtils.getInstance().SavePhoto(data, mContext); //this returns an AdobeImageIntent
startActivityForResult(intent, 1);
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
//this never gets called, because activity finishes, but why?
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
So why is the activity finishing, if the other one does not?
Turns out this was my fault, I did not notice I had android:noHistory="true"
in the manifest for the second activity

Custom RecyclerAdapter and startActivityForResult

I have a Fragment that contains a RecyclerView which uses a custom RecyclerAdapter. I have an onClickListener inside my custom RecyclerAdapter - when a position is clicked I want it to start startActivityForResult. So far this works in as such as when it is clicked it starts the Activity as desired. However when I press the back button to go to the Fragment containing the RecyclerView onActivityResult is never called. I have passed in a context to the custom RecyclerAdapter. Is this something that is possible? Or does the Activity/Fragment initiating startActivityForResult be the one that intercepts it? If not I will end up handling the onClick in the Fragment with a gesture detector or something similar, but before that I wanted to give this a fair crack! Note: I have included onActivityResult in the MainActivity which has the Fragment container so the Fragment does receive onActivityResult if startActivityForResult is initiated from the Fragment. My code:
RecyclerAdapter onClickListener:
#Override
public void onClick(View v) {
String titleId = titlesListDataArrayList.get(getLayoutPosition()).getTitle_id();
Intent intent = new Intent(context, CreateItemsActivity.class);
intent.putExtra("TITLE_ID", titleId);
((Activity) context).startActivityForResult(intent, Constants.NEW_ITEMS_REQUEST_CODE);
}
CreateItemsActivity.class - onBackPressed()
#Override
public void onBackPressed() {
Intent intent = new Intent();
setResult(Constants.NEW_ITEMS_REQUEST_CODE, intent);
finish();
}
MyListsFragment.class (contains RecyclerView)
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.e("CALLED", "OnActivity Result");
// if requestCode matches from CreateItemsActivity
if (requestCode == Constants.NEW_ITEMS_REQUEST_CODE) {
Log.e("REQUEST CODE", String.valueOf(requestCode));
populateRecyclerView();
}
}
Also I have this in the MainActivity:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// included to allow fragment to receive onActivityResult
}
Your calling Activities onActivityResult will only be called when the second activity finishes and a setResult has been executed.
Since the user is hitting the back button the 2nd activity is finishing without setResult being called.
You'll need to override onBackPressed so you can execute your setResult code.
I see you have implemented this but I think the crux of the issue is that you need to return Activity.RESULT_OK not your request code.
#Override
public void onBackPressed() {
Intent intent = new Intent();
setResult(RESULT_OK, intent);
super.onBackPressed();
}
In this case you don't need to explicitly return your requestCode of Constants.NEW_ITEMS_REQUEST_CODE because Android will forward that automatically.
OK, so IF by any chance somebody else has this problem here is my
solution. I added this code into the MainActivity onActivityResult (note I have a frame container which is where all fragments are inflated):
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// get current fragment in container
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.frameContainer);
fragment.onActivityResult(requestCode, resultCode, data);
}
I believe this works as the MainActivity is top in the hierarchy and intercepts the onActivityResult, so basically I just point it where I want it to be used.

Android Studio Facebook: onActivityResult in normal class

In order to listen to a facebook login event I must override in my activity the function onActityResult and make a call to facebook's callbackmanager.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
So far everything's working.
I'd like to create a static class for facebook that handles facebook callbacks, such as login, etc.
Is there substitute for onActivityResult in that case?
You mean you want another class to handle the activity returning? Then you have to have onActivityResult of the activity that calls startActivityForResult call into your facebook class. There's no way to reroute it automatically.

Categories

Resources