There are many question here from people having the exact same problem as I, and I've looked though a million, tried different things for 2-3 hours now, and I still can't get it working.
Child Activity:
Intent resultIntent = new Intent(myColorPicker.this, WidgetConfig.class);
resultIntent.putExtra("key", appwidget_notecolor);
setResult(RESULT_OK, resultIntent);
finish();
Parent Activity:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Toast does not show
Toast.makeText(getApplicationContext(), "onActivityResult fired", Toast.LENGTH_SHORT).show();
if (resultCode == RESULT_OK ) {
// Toast does not show
Toast.makeText(getApplicationContext(), "Result Recieved", Toast.LENGTH_SHORT).show();
}
}
I launch child activity from parent activity like this:
Intent myColorPickerIntent = new Intent(WidgetConfig.this, myColorPicker.class);
myColorPickerIntent.putExtra("appwidget_notecolor", appwidget_notecolor);
WidgetConfig.this.startActivity(myColorPickerIntent);
Of course you won't get the result, you're calling startActivity() instead of startActivityForResult().
You don't seem to be calling startActivityForResult() after creating the Intent.
Are you pass the intent to startActivityForResult() method?
Related
I am trying to get result for dialer Intent using startActivityForResult()
Below is my code for Dialer Intent.
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:123456789"));
startActivityForResult(intent, 1234);
}
});
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 1234){
if (resultCode == Activity.RESULT_OK){
Toast.makeText(getApplicationContext(), "result ok", Toast.LENGTH_LONG).show();
}else if (resultCode == Activity.RESULT_CANCELED){
Toast.makeText(getApplicationContext(), "Result Cancelled", Toast.LENGTH_LONG).show();
}
}
}
whenever I am returning to my activity, Result Cancelled Toast is triggering.
Thanks in advance.
From doc:
ACTION_DIAL
public static final String ACTION_DIAL
You only have the ACTION. If you want to call a number from your application then you just have to put these lines of code into the onClick() method and can get what you want:
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:123456789"));
startActivity(intent); // no need to use startActivityResult(intent,1234)
Here, If an ACTION_DIAL input is nothing, an empty dialer is started; else getData() is URI of a phone number to be dialed or a tel: <yourURI> of an explicit phone number.
Additionally, there is no "Output" of RESULT_OK or RESULT_CANCELED, Cause, the startActivityResult() doesn't mean anything to ACTION_DIAL but startActivity(intent). Hope it helps.
why am I getting RESULT_CANCELED instead of RESULT_OK.
ACTION_DIAL does not return a result. If you read the documentation for ACTION_DIAL, you will see "Output: nothing". Hence, you will usually get RESULT_CANCELED. Only activities designed for use with startActivityForResult() will return a result code.
I have 2 Activity classes and 1 Non-Activity class which calls startActivityForResult() from Context passed in constructor. This is how it looks: FirstActivity -> NonActivity -> SecondActivity -> FirstActivity. In SecondActivity there is ArrayList of custom objects that needs to be passed to FirstActivity as a result. There is a problem. When onActivityResult() is called resultCode is RESULT_CANCELED, but not RESULT_OK even if setResult(RESULT_OK, intent) is called. Here is my code:
NonActivity
public void showActivity() {
Intent intent = new Intent(request, ActivityKorak.class);
intent.putExtra("data", fields);
request.startActivityForResult(intent, 1);
}
SecondActivity
#Override
public void onBackPressed() {
super.onBackPressed();
Intent intent = new Intent();
intent.putExtra("data", fields);
setResult(Activity.RESULT_OK, intent);
finish();
}
FirstActivity
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent){
super.onActivityResult(requestCode, resultCode, intent);
if(resultCode != Activity.RESULT_CANCELED){
if(requestCode == 1) {
Bundle extras = intent.getExtras();
ArrayList<CustomInput> fields = (ArrayList<CustomInput>) extras.getSerializable("data");
}
}
}
You must simply remove
super.onBackPressed();
in the onBackPressed Method
What is happening is that "super.onBackPressed()" is setting the result code to "RESULT_CANCELED" and finishing your activity.
In my case, the problem was fixed by adding the SHA-1 and SHA-256 certificate fingerprints given in Android Studio (click on Gradle on the right side of the AS window, then run configurations and signingReport) to your Firebase project settings->General->SDK setup and configuration.
Update
Activity A ==> host MyFragment and from here,user started ==> ActivityB from here I need to get some value from this activty for example value integer and some String to MyFragment that hosted in ActivityA
I've MyFragment that start the activityB like this :
Intent intent = new Intent(getActivity(),NewsCommentActivity.class);
intent.putExtra("MNewsFeed", new Gson().toJson(newsFeed));
intent.putExtra("idCommentBadge",idCommentBadge);
startActivityForResult(intent,addComment);
and then when user start the activty and finish it I'm trying to pass some value from that ActivityB to MyFragment like this :
#Override
public void onBackPressed() {
super.onBackPressed();
//set ok result before finish the activity
Intent returnIntent = new Intent();
returnIntent.putExtra("idBadgeComment", idBadgeComment);
returnIntent.putExtra("totalCommentInserted", totalCommentInserted);
setResult(RESULT_OK, returnIntent);
finish();
}
and I've implement onActivityResult in MyFragment :
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.i(TAG, "Result from News Comment ");
if(resultCode== Activity.RESULT_OK){
Log.i(TAG, "Result from News OK");
if(requestCode==addComment){
Log.i(TAG, "Result ReqCode Oke");
int idBadgeComment = data.getExtras().getInt("idBadgeComment");
int totalCommentInserted = data.getExtras().getInt("totalCommentInserted");
Log.i(TAG, "idComment: "+idBadgeComment);
Log.i(TAG, "Total Comment Inserted: "+totalCommentInserted);
}
}
}
also I've called onActivityResult in ActivityA that hosted MyFragment :
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
/**
* call below code to get the result on the fragment
*/
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.viewpager);
fragment.onActivityResult(requestCode, resultCode, data);
}
when I try the code above I only get Log.i(TAG, "Result from News Comment "); and it didn't get the RESULT_OK value and the Intent data is null, can someone please pointed out where did I go wrong? and how to pass the value from activity to fragment ? or maybe a better way instead of using onActivityResult?
You can try putting super.onBackPressed(); as the last line since RESULT_CANCELED is returned on back press.
Just startActivity by activity.
Intent intent = new Intent(getActivity(),NewsCommentActivity.class);
intent.putExtra("MNewsFeed", new Gson().toJson(newsFeed));
intent.putExtra("idCommentBadge",idCommentBadge);
getActivity().startActivityForResult(intent,addComment);
Or put your onActivityResult to fragment.
I have two Activities: ActivityA and ActivityB.
I start ActivityB with startActivityForResult:
Intent lIntent = new Intent(ActivityA.this, ActivityB.class);
startActivityForResult(lIntent, 1);
I have this code to go back from ActivityB to ActivityA(ActivityB can call itselfs many time):
Intent intent = new Intent(ActivityB.this,ActivityA.class);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("files",
mChecked.toArray(new String[mChecked.size()]));
startActivity(intent);
//setResult(Activity.RESULT_OK, intent);
That code runs fine without setResult. My problem is I have to set RESULT_OK to deal correctly with the result in ActivityA:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1){
if (resultCode == Activity.RESULT_OK){ <-- always 0 and not -1
System.out.println("OK");
}
}
}
if I comment in setResult in ActivityB, onActivityResult is never called.
If I use startActivity, onActivityResult is called but the resultCode is not set.
How can I set the result code in my case?
UPDATE
Intent intent = new Intent(FileChooserActivity.this,FtpContentListView.class);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("files", mChecked.toArray(new String[mChecked.size()]));
setResult(Activity.RESULT_OK, intent);
startActivity(intent);
with that code onActivityResult is called but the resultCode is 0 and not -1(RESULT_OK).
Thanks a lot for your help
Stefan
I believe what you're after is to first start Activity B for result (as you are currently), then subsequently starting new instances of Activity B using the Intent.FLAG_ACTIVITY_FORWARD_RESULT. Note however, that you would need to finish previous instances of Activity B whenever you create new ones (as described in this gist https://gist.github.com/mcelotti/cc1fc8b8bc1224c2f145).
Alternatively, you could simply start all instances of Activity B for result and pass down the result until Activity A was reached:
#Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
setResult(resultCode);
finish();
}
You need to make a change in Activity-B :
Bundle b = new Bundle();
b.putStringArray("files",
mChecked.toArray(new String[mChecked.size()]));
Intent intent = new Intent(ActivityB.this,ActivityA.class);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtras(b);
setResult(RESULT_OK, intent);
finish();
Hope this works.
I have 2 Activities the main activity I have called Map activity and the second that I have called Question activity when the app launch the Map activity is shown then you click the play button and the Question activity is launched with StartActivityForResult(), then when you have answered the question right the Question activity should be destroyed and created again and check the number of right answered questions and change the layout. then if you have scored 5 then Question activity should be destroyed and the Map activity will be shown but this is not happen. Here is my code
Map.class
public void OnClick_Question(View v){
Intent i = new Intent(MapActivity.this, QuestionActivity.class);
startActivityForResult(i, 1);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.d(SharedPrefs.TAG, "OnActivityResult Entry");
if (resultCode == RESULT_OK) {
Log.d(SharedPrefs.TAG, "MAPActivity OnActivityResult Entry resultCode");
AssignContentView(requestCode);
}
}
Question.class
public void Next() {
Intent i = new Intent(this, QuestionActivity.class);
startActivity(i);
finish();
}
public void LevelUp() {
Log.e(SharedPrefs.TAG, "LevelUp");
super.finish();
finish(); /*This don't happen*/
}
When I see LogCat shows me this message:
09-01 11:23:03.911 901-1485/? W/ActivityManager﹕ startActivity called from finishing ActivityRecord{43386100 u0 com.example.gbb/.QuestionActivity t50 f}; forcing Intent.FLAG_ACTIVITY_NEW_TASK for: Intent { cmp=com.example.gbb/.QuestionActivity }
Why I am doing this, is because I want to change the layout of Map Activity depending in your score.
What I'm doing wrong?
I had the same problem. I solved it by adding android:launchMode="singleTask" in the Android Manifest. Hope it works for you too.
I solved this by turning off Instant Run and re-running my code.
You can try the below code:
We need to keep the request code to be in global and static variable to access in onActivityResult method
private static final int REQUEST_CODE = 111;
public void OnClick_Question(View v){
Intent i = new Intent(MapActivity.this, QuestionActivity.class);
startActivityForResult(i, REQUEST_CODE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
Log.d(SharedPrefs.TAG, "OnActivityResult Entry");
if (resultCode == RESULT_OK) {
switch (requestCode) {
case REQUEST_CODE:
Log.d(SharedPrefs.TAG, "MAPActivity OnActivityResult Entry resultCode");
AssignContentView(requestCode);
break;
default:
break;
}
}
}
I think there is no problem in your Map class, the problem is in second class.i.e., you need to set the RESULT_OK in that intent and then you can check if (resultCode == RESULT_OK) in onActivityResult method. Below you can find the code for that
Intent intent = new Intent();
setResult(activity.RESULT_OK, intent);
finish();
Hope this is helpful:)