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.
Related
I have the main page where the photo is loaded, depending on what the array on the other page contains, in order to synchronize them I used StartActivityForResult();.
It will works like this: MainActivity has a photo, i press showMore button(open showMoreActivity), at showMoreActivity change text and after i finish showMoreActivity, MainActivity load the new photo depends on new text, but real it doesn`t change photo.
Can you help me? Where the mistake is?
buttonShowMore.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), ShowMore.class);
onStop();
getActivity().startActivityForResult(intent, 1);
}
});
onClick button ShowMore Listener
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (data == null) {return;}
String dataStringExtra =data.getStringExtra("name1");
Picasso.get().load( dataStringExtra + ".jpg").into(imageViewFirstOfCurrentList);
}
onActivityResult method
Intent intent = new Intent();
intent.putExtra("name1", List.get(0).toString());
setResult(RESULT_OK, intent);
finish();
when close showMore Activity
try removing call to onStop() in your onClick() method
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.
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 button in Fragment when I press it I open a new activity for result but When I return back to my fragment I found all UI element = null
Please find the code
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), MyActivity.class);
getActivity().startActivityForResult(intent, "3030");
}
});
when choose a value from activity I should back to fragment and set data to textview in the activity.
Intent intent = Activity.this.getIntent();
intent.putExtra("categoryId", id);
intent.putExtra("categoryName", name);
setResult(RESULT_OK, intent);
finish();
and I have put that in the activity that contains the fragment
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 3030 && resultCode == RESULT_OK) {
Fragment fragment = mTabFragments.get(MyFragment.class.getName());
if (fragment != null) {
fragment.onActivityResult(requestCode, resultCode, data);
}
}
}
and in fragment
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 3030 && resultCode == Activity.RESULT_OK) {
int categoryId = data.getIntExtra("categoryId", 0);
String categoryName = data.getStringExtra("categoryName");
mChooseCategoryTextView.setText(categoryName);
}
}
the problem now that mChooseCategoryTextView is null
Can anyone tell me what is the problem?
To get result in fragment
startActivityForResult(intent,REQ_CODE);
not
getActivity().startActivityForResult(intent,REQ_CODE);
I think the question how you initialise this view?
Since your lunching another activity so your whole fragment my be reconstruct or only onViewCreated recalled again.
So my guess is that you don't reinitialize or override mChooseCategoryTextView reference in one of fragment callbacks.
Try to add more logs and check what's happening for this reference
I believe the error is in the line
Fragment fragment = mTabFragments.get(MyFragment.class.getName());
I assume mTabFragments in an adapter of some sort? I'd have to look at it's code to be sure, but it sounds like it's not returning the right fragment. Make sure that the reference it's returning is the same as the fragment that is being shown on screen.
I am currently trying to send data from a child activity back to its parent activity. I have used the startActivityForResult() to go to my child activity. However I cannot understand what I would do to put the data I want to put back to my intent and make my parent activity receive it. I have been looking at different examples online but I think it is only throwing the result variable.
As per my understanding this is what I used when I return my child activity back to the parent activity:
String somestring = "somevalue";
Intent i = getIntent();
setResult(RESULT_OK, i);
finish();
I want to load the content of the string somestring for it to return back to the parent activity.
How can I load it back to my parent activity?
startActivityForResult(intent, 1);
And lastly how can I capture the data in my parent activity in the onActivityResult?
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode,resultCode,data);
}
Add your data to an intent, just as you would in any other situation:
Intent intent=new Intent();
intent.putExtra("ComingFrom", "Hello");
setResult(RESULT_OK, intent);
finish();
Then retrieve it in onActivityResult in your other activity.
#Override
public void onActivityResult(int requestCode,int resultCode,Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
String extraData=data.getStringExtra("ComingFrom"));
}