I am trying to use an intent to import a file form the device to my app. I am using Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
My question is... I know for the intent I need to have onActivityResult() method, but I already have one for another intent being used the the class, is there a way I can have two onActivityResult() methods for two intents.
onActivityResult(int requestCode, Result resultCode, Intent data)
use differnet requestCode for different intents
startActivityForResult(intent, requestCodeForIntentOne);
startActivityForResult(intent, requestCodeForIntentTwo);
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if(requestCode==requestForIntentOne)
{}
if(requestCode==requestForIntentTwo)
{}
}
when you call startActivityForResult(), you supply a requestCode. That will match the value of the same name you get in onActivityResult()
So you can do :
if (requestCode == requestCodeA) {
//handle case 1
} else if (requestCode == requestCodeB) {
//handle case 1
}
Related
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
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.
I've got a problem I don't know how to solve.
This is my code of the MainActivity.java which starts a new activity:
Intent intent = new Intent(this, AssociationActivity.class);
intent.putExtra("data", json);
startActivityForResult(intent, 0);
Additionaly, I have the following onActivityResult()-method:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
System.out.print("works outside!");
if (requestCode == 1) {
if(resultCode == RESULT_OK){
System.out.print("works inside!");
}
if (resultCode == RESULT_CANCELED) {
}
}
}
And this is where the result should be sent to the MainActivity:
Intent returnIntent = new Intent();
returnIntent.putExtra("result", string);
setResult(RESULT_OK, returnIntent);
finish();
It seems that the onActivityResult() doesn't even get called as I don't have any output on the console.
In the AndroidManifest.xml I neither use the singleIntent nor the noHistory parameter.
Any suggestions why it doesn't work?
requestCode change from 1 to 0. as shown below
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
System.out.print("works outside!");
if (requestCode == 0) {
if(resultCode == RESULT_OK){
System.out.print("works inside!");
}
if (resultCode == RESULT_CANCELED) {
}
}
}
Okay, I found the issue, the wasn't any issue at all, I replaced the System.out statements with the Log from Android it's working all the time, Android just delayed the System.out, but I don't know why.
Thanks #Rene Juuse for that tip!
In my MainActivity, I launch a second activity:
Button button = (Button) findViewById(R.id.btnPush);
button.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
Intent nowStart = new Intent(getApplicationContext(), AddPillScheduleActivity.class);
startActivityForResult(nowStart, RESULT_OK);
//startSecond();
}
});
Then inside my Second Activity, I would like to return a value back to the main activity.
Intent i=new Intent();
i.putExtra("ANSWER", ans);
setResult(RESULT_OK,i);
finish();
That seems to execute fine, but back in my MainActivity, I would like to grab the value. This is where I am having trouble. My debugger never stops on my onActivityResult, which is:
#Override
protected void onActivityResult(int requestCode ,int resultCode ,Intent data ) {
super.onActivityResult(requestCode, resultCode, data);
String name = getIntent().getExtras().getString("ANSWER");
if (resultCode == RESULT_OK) {
Toast.makeText(this, name, Toast.LENGTH_LONG).show();
}
Can someone shed a little light? Thanks.
You're not getting the value from the right intent, the one that comes with the method. Modify your code to the following:
#Override
protected void onActivityResult(int requestCode ,int resultCode ,Intent data ) {
super.onActivityResult(requestCode, resultCode, data);
String name = data.getStringExtra("ANSWER");
if (resultCode == RESULT_OK) {
Toast.makeText(this, name, Toast.LENGTH_LONG).show();
}
}
Use the getStringExtra method as you put a String in your intent and not a bundle. Also, not the best practice use the same code for the requestCode and resultCode.
String name = getIntent().getExtras().getString("ANSWER");
is accessing the wrong intent. getIntent() returns the intent that started the activity. You want
String name = data.getExtras().getString("ANSWER");
Also, the second parameter of startActivityForResult() is a request code, so using RESULT_OK -- althought it still works -- is confusing.
I have a main activity that calls another(2nd) activity "VennDiagram"
Intent intent = new Intent(getApplicationContext(), VennDiagram.class);
startActivityForResult(intent, 200);
the 2nd activity after doing some calculations calls a 3rd activity "ImageMapTest...." acitvity
Intent i = new Intent(getApplicationContext(), ImageMapTestActivity.class);
i.putExtras(sendBundle);
startActivity(i);
//finish();
Now I need help to return the an arrayList from the 3rd activity to the first ??!!
Intent in = new Intent(getApplicationContext(), AndroidClientActivity.class);
in.putExtra("songList", playList);
setResult(200, in);
finish();
I do have an intent listener as below at my 1st activity, but its just that the 3rd activity won't send it back to it
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == 100 || resultCode == 200)
{
}
P.S I did read couple of similar posts but they were not what I was looking for...
You should be able to just send the result received by B from C back to A.
protected void onActivityResult(int requestCode,int resultCode, Intent data) {
super.onActivityResult(requestCode, data);
setResult(resultCode,resultData);
finish();
}
Finish C and return your array to B, and then when you return to B finish and return the result back to A.