I am facing a strange issue while returning to an Activity with a Result, I am passing an Intent for startActivityForResult from an Adapter like this :
Intent i = new Intent(activity, EditInfoActivity.class);
i.putExtra("id", list.get(position).getID());
activity.startActivityForResult(i, 100);
and in second Activity i.e. in EditInfoActivity in my case on a Button click I am setting Result for first activity like this:
Intent i = getIntent();
i.putExtra("isDataChange", isDataChange);
setResult(100, i);
finish();
In Activity's onActivityResult method I am able to get result code but getting Intent null.
Why? anybody have any idea on this please share.
in Activity:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 100) {
//Here data is null and app crash
if (data.getExtras() != null && data.getBooleanExtra("isDataChange", false)) {
recreate();
}
}
}
First, you need to start the Activity with a REQUEST_CODE:
// Here we set a constant for the code.
private final int REQUEST_CODE = 100;
Intent i = new Intent(activity, EditInfoActivity.class);
i.putExtra("id", list.get(position).getID());
activity.startActivityForResult(i, REQUEST_CODE);
Then you need to send RESULT_OK when finishing EditInfoActivity:
Intent i = getIntent();
i.putExtra("isDataChange", isDataChange);
setResult(RESULT_OK, i);
finish();
Then handle the result on your first activity with this:
Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// REQUEST_CODE is defined as 100
if (resultCode == RESULT_OK && requestCode == 100) {
// do process
}
}
setResult TAKES RESULT_CODE instead of REQUEST_CODE.
Replace your code with this, May be it will solve your problem.
setResult(RESULT_OK, i);
And in yout onActivityResult
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
//Here data is null and app crash
if (data != null && data.getBooleanExtra("isDataChange", false)) {
recreate();
}
}
}
Two mistakes. You are passing the intent that was used to launch the activity you are finishing. Use new Intent() instead.
When setting activity result you should use result codes, not a request code setResult(RESULT_OK) alternatively RESULT_CANCELED and handle the response accordingly.
Related
I have two activities. The second activity is called from the first using startActivityForResult(intent,request_code).
In the second activity I have this code:
Intent i = getIntent();
i.putExtra("data" , some data);
setResult(Activity.RESULT_OK,i);
finish();
Then, to get the data in the first activity I use this code:
#Override
protected void onActivityResult(int requestCode,int resultCode,Intent intent)
{
super.onActivityResult(requestCode, resultCode, data);
if (requestCode==this.uploadRequestCode && resultCode == Activity.RESULT_OK)
{
data = intent.get("data")
}
I start the activity this way:
Intent uploadIntent = new Intent(this,uploadActivity.class)
startActivityForResult(uploadIntent,this.uploadRequestCode)
The problem is that the result code I get is RESULT_CANCELLED even though I set RESULT_OK.
NOTE
I am not talking about a situation when back button is pressed.
UPDATE
I found out that the intent I get in onActivityResult() is null even though I sent an intent, that's why the result code was RESULT_CANCELLED.
Anyone knows why the intent is null?
Maybe this can help you out, something from a project that I have
This how I do start my activity
Intent intent = new Intent(FirstActivity.this, ActivityThatReturnInfo.class);
startActivityForResult(intent, 1);
This is from the activity that I want to return some value
Intent returnIntent = new Intent();
returnIntent.putExtra("VAR",someInfo);
setResult(Activity.RESULT_OK,returnIntent);
finish();
This is on the activity that as started the one that is waiting for the information
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
switch (requestCode)
{
case 1:
if(resultCode == Activity.RESULT_OK)
{
String result = data.getStringExtra("VAR");
// Code to do
}
break;
default:
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if(result != null)
{
if(result.getContents() != null)
{
// Code to do
}
}
break;
}
}
Starting SecondActivity from FirstActivity
Intent i = new Intent(this, SecondActivity.class);
startActivityForResult(i, 100);
Sending data to FirstActivity from SecondActivity
Intent intent= new Intent();
intent.putExtra("result",result);
setResult(RESULT_OK,intent);
finish();
getting result in FirstActivity
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == 100 && resultCode == Activity.RESULT_OK){
String result=data.getStringExtra("result");
Log.e("Result",result);
}
}
Edit:
Change your code to below and try once.
While sending intent from SecondActivity to FirstActivity Intent i = getIntent(); to Intent intent= new Intent();
Here is my code:
final Intent intent = new Intent(this, SelectWidgetActivity.class);
startActivityForResult(intent, REQUEST_CREATE_APPWIDGET);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK ) {
String selectedName = data.getStringExtra(SelectWidgetActivity.SELECTED_WIDGET);
if(!mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId, info.provider)) {
Intent bindIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
bindIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
bindIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.provider);
bindIntent.putExtra(SelectWidgetActivity.SELECTED_WIDGET, selectedName);
startActivityForResult(bindIntent, REQUEST_BIND_APPWIDGET);
}
}
}
I put String on the first call of startActivityForResult and on the second call I'm trying to get the same string, but getting null. For example on the first call selectedName="Calendar", the second call onActivityResult() selectedName=null
Any ideas?
you must be even checking for the requestCode in onActivityResult that you send when you are making this call
startActivityForResult(intent, REQUEST_CREATE_APPWIDGET);
Try this
if (requestCode == REQUEST_CREATE_APPWIDGET && resultCode == Activity.RESULT_OK) {
//do your work here;
}
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.
This question already has answers here:
How to manage startActivityForResult on Android
(14 answers)
Closed 8 years ago.
I am new to android development
I call an intent now how can i get result from called activity
can any one tell me how to perform this task ?
i have called intent like.
Intent I = new Intent (this ,abc.class);
startActivity(i);
thanks
Use startActivityForResult and then override onActivityResult in your FirstActivity.
In FirstActivity
Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
startActivityForResult(intent, 2);// Activity is started with requestCode 2
Override onActivityResult
#Override
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 2
if(requestCode==2)
{
String message=data.getStringExtra("MESSAGE");
Log.i("Message is",message);
// logs Testing
}
}
In SecondAcivity
Intent intent=new Intent();
intent.putExtra("MESSAGE","Testing");
setResult(2,intent);
finish();//finishing activity
Reference to the docs:
http://developer.android.com/reference/android/app/Activity.html#startActivityForResult(android.content.Intent, int)
Example:
http://www.javatpoint.com/android-startactivityforresult-example
For going to second activity use startActivityForResult in your firstclass
Intent callIntent = new Intent(FirstClass.this, SecondClass.class);
startActivityForResult(callIntent, 1);
then override onActivityResult method in your first class like this
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Check which request we're responding to
if (requestCode == 1) {
// Make sure the request was successful
if (resultCode == RESULT_OK) {
// get values from data
}
} }
In your second class do this for returning back, if you want to send
something to first class. Store this in your intent.
Intent result = new Intent(); setResult(Activity.RESULT_OK, result);
finish();
hi you can get result calling activity
Start activity with startActivityForResult(intent, 0);
add below method in calling activity
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
// do your task
} else if (resultCode == RESULT_CANCELED) {
// do your task
}
}
}
In your main Class....
static final int CODE_REQUEST = 1; // The request code
....
Intent pickContactIntent = new Intent(MainClass.this, CallingClassName.class);
startActivityForResult(pickContactIntent, CODE_REQUEST);
..........
#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)
}
}
}
In your calling class
Intent result = new Intent();
setResult(Activity.RESULT_OK, result);
finish()
When I my app is launched I am showing a splash screen. That page is been shown for 10 sec, running on a thread.
When it switches over to new activity on a result I want o hit a URL in server and I will be getting a return value which I can use for my further implements.
Here is my code:
private final int SPLASH_DISPLAY_LENGHT = 1000;
new Handler().postDelayed(new Runnable()
{
#Override
public void run()
{
Log.e("Handler ","run");
Intent myIntent = new Intent(getApplicationContext(), CaptureActivity.class);
startActivityForResult(myIntent, imgDL);
finish();
}
}, SPLASH_DISPLAY_LENGHT);
public void onActivityResult(int requestCode, int resultCode, final Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == imgDL)
{
Log.e("onActivity Result","");
urlHitMethod("http://XXXXXXXXXXXXXXXXXX.com/banner_scan");
}
}
But here the onActivityResult is not called. How to fix this?
Also, please note than if you base activity (the one calling startActivityForResult) can't use the flag noHitory in the manifest.
If you do so, onActivityResult will never be called.
try this
Intent myIntent = new Intent(activity.this, CaptureActivity.class);
and
#Override
public void onActivityResult(int requestCode, int resultCode, final Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == imgDL)
{
Log.e("onActivity Result","");
urlHitMethod("http://XXXXXXXXXXXXXXXXXX.com/banner_scan");
}
if(resultCode==RESULT_OK)
{
Log.e("onActivity Result","come in onactivity result ok");
}
else
{
Log.e("onActivity Result","come in onactivity result with error");
}
}
If you are using onActivityResult, then you should not finish the activity when starting with intent otherwise it will crash the app.
Thanks.
In the CaptureActivity.class you have to set the result and then check in the onActivityResult in the First Activity the result code
In the CaptureActivity.class it should be like the following
Intent in = new Intent();
setResult(1,in);//Here I am Setting the Requestcode 1, you can put according to your requirement
finish();