I have been fighting with this problem for hours now and have looked at pretty much every other post about this issue but can't find where I am going wrong. Please let me know if I am missing something simple.
Activity 1 ( hosts a CreateFragment)
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
((CreateFragment) getFragmentManager().findFragmentByTag(CreateFragment.TAG)).resultReturned(requestCode, resultCode, data);
}
CreateFragment:
private void chooseContact() {
Intent chooseContactInappIntent = new Intent( getActivity(), ContactPickerActivity.class );
getActivity().startActivityForResult( chooseContactInappIntent, PICK_CONTACT_REQUEST );
}
public void resultReturned( int requestCode, int resultCode, Intent data ) {
switch ( requestCode ) {
case PICK_CONTACT_REQUEST:
if ( resultCode == Activity.RESULT_OK ) {
//Do Cool Things
default:
break;
}
}
ContactPickerActivity:
#Override
public void onContactSelected( Object data) {
Intent returnIntent = new Intent();
returnIntent.putExtra("data",data);
setResult(RESULT_OK,returnIntent);
finish();
}
Activity One hosts a CreateFragment, when the use clicks a button in the CreateFragment the fragment calls the chooseContact() method which starts a new ActivityForResult. The ContactPickerActivity then displays the user a list of contacts. When the user selects a contact the onContactSelected method is called creating a new intent to pass back the selected data and then calls finish. I thought this would either call the onActivityResult in the base activity or the CreateFragment, but neither are called.
Any Ideas?
Thanks,
Nathan
In Fragment class work fine if you call at MainActivity onActivityResult override method and you don't need to changes inside the fragment class.
So
In Fragment that calls startActivityForResult like this:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && (requestCode == GALLERY_REQUEST || requestCode == CAMERA_REQUEST)) {
// its not complicated .....
}
}
In MainActivity Class are must to call onActivityResult Also.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
for (Fragment fragment : getSupportFragmentManager().getFragments()) {
fragment.onActivityResult(requestCode, resultCode, data);
}
}
Related
I have a hierarchy like this in my android app.
ProfileActivity
SettingTabFragment
ProfileContentFragment
From inside ProfileContentFragment I call another activity called ProfileVideoRecordingActivity using startActivityForResult()
Here is the code in ProfileContentFragment
Intent videoRecordIntent = new Intent(mActivity, ProfileVideoRecordingActivity.class);
startActivityForResult(videoRecordIntent, REQUEST_CODE_PROFILE_VIDEO_PATH);
Here is how I return from ProfileVideoRecordingActivity
Intent intent = new Intent();
intent.putExtra(VIDEO_PROFILE_PATH, renamedVideoFile.getAbsolutePath());
setResult(Activity.RESULT_OK, intent);
//Go back to calling Activity
finish();
Problem is neither ProfileActivity's nor ProfileContentFragment's onActivityResult() is called.
ProfileActivity
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
}
SettingTabFragment
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
}
ProfileContentFragment
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == REQUEST_CODE_PROFILE_VIDEO_PATH){
if(resultCode == Activity.RESULT_OK) {
String profileVideoPath = data.getExtras().getString(ProfileVideoRecordingActivity.VIDEO_PROFILE_PATH);
Log.d("DEBUG", profileVideoPath);
}
}
}
I tried solutions from stackoverflow where I need to call fragment's onActivityResult explicitly from activity but that didn't work.
Here is what I tried.
ProfileActivity inside onActivityResult()
List<Fragment> fragments = getSupportFragmentManager().getFragments();
if (fragments != null) {
for (Fragment fragment : fragments) {
fragment.onActivityResult(requestCode, resultCode, data);
}
}
SettingTabFragment inside onActivityResult()
List<Fragment> fragments = getChildFragmentManager().getFragments();
if (fragments != null) {
for (Fragment fragment : fragments) {
fragment.onActivityResult(requestCode, resultCode, data);
}
}
Both of them didn't work. What can I do?
im using tabbaractivity and one of the tabs contains listView
i want to update the listView every time that i adding object.
to add object i do it from new activity
what i wants is when this activity will close the listView will update by using
adapter.notifydatachanged
in my activity that contains the tabs
if (id == R.id.addButton) {
Intent intent = new Intent(rootViewTabBarActivity.this, addPictureActivity.class);
startActivityForResult(intent,101);
return true;
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}
in the fragment (one of the tabs)
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if ((requestCode == 101) && (resultCode == Activity.RESULT_OK))
adapter.notifyDataSetChanged();
}
and the activity that create the object and save him to database
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAM_REQUEST) {
Bitmap btmp = (Bitmap)data.getExtras().get("data");
takenPhoto.setImageBitmap(btmp);
}
}
#Override
protected void onDestroy() {
super.onDestroy();
setResult(Activity.RESULT_OK);
}
Thanks
fragment not called to onActivityResult
You miss passing the data from Activity to fragment
In Activity, your code should be like this:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
YourFragment fragment = getFragment();
if (fragment != null) {
// Pass data to Fragment
fragment.onActivityResult(requestCode, resultCode, data);
}
}
}
public YourFragment getFragment() {
// You can find your fragment (by tag/ by id) here depends on your layout
}
i have a tab activity in which i have an activity and in that Activity i have a Fragment..
in tabActivity:-
TabActivity
{
#ovverride
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
}
}
in Activity:-
Activity
{
#ovverride
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
}
}
in Fragment:-
Fragment
{
void startActivityForResult()
{
startActivityForResult(intent,requestCode);
}
#ovverride
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
//doing my work here
}
}
now the problem is when i had used support Fragment then i do not need to have #ovverride onActivityResult in Activity or in TabActivity it was working very fine..but now i have android.app.Fragment and i have tried everything but i could not able to get OnActivityResult in my app Fragment...
please solve this..i'll be a great help..
try like this may help you,
Explicit call from fragment to onActivityResult function as follows
In Parent Activity class, override the onActivityResult() method and even override the same in Fragment Class and call as the following code.
In Parent Class:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.dualPane);
fragment.onActivityResult(requestCode, resultCode, data);
}
In fragment
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//in fragment class callback
}
Here's my code:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == LOGIN_REQUEST) {
if(resultCode == RESULT_OK) {
//Do Stuff
} else if(resultCode == RESULT_CANCELED) {
moveTaskToBack(true);
}
return;
}
super.onActivityResult(requestCode, resultCode, data);
}
After moveTaskToBack(true) my app is backgrounded, but onActivityResult gets called every time I try to open my app, which immediately backgrounds it again. How do I signal that I've handled the activity result and don't want to be notified about it again?
It should be noted, that I tried this with
super.onActivityResult(requestCode, resultCode, data);
being the first thing in the method as well, same problem.
You probably need something like this, instead of doing it on the onActivityResult method:
#Override
public void onBackPressed() {
moveTaskToBack(true);
}
I'm working on a project with android 1.6 as target.
So, I can't use
#Override public void onBackPressed(){...}
I'm starting an activity with
startActivityForResult(intent,requestcode)
And I wan't to get back some info with
#Override protected void onActivityResult (int requestCode, int resultCode, Intent data)
the problem is the following:
-'Activity A' throws 'Activity B'
-during 'ActivityB' the hardware back button is pressed
-'Activity A's onActivityResult is called but I don't get any info in data (data==null)
I'm trying to put some extra info at 'Activity B's
#Override protected void onPause(){...}
I also call setResult(RESULT_OK,i); into this onPause but I always get RESULT_CANCELED and data==null at 'Activity A's onActivityResult
Instead of onBackPressed you can use:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
//set result and finish()
} else {
return super.onKeyDown(keyCode, event);
}
}
I don't see the values of the constants you pass to and fro. And they are important.
Here are some pieces of my working code that calls an activity for result:
------------------------------------- PackVideo activity --------------------------------------
calling for ServerSetActivity
Intent serverSetIntent = new Intent();
serverSetIntent.setClass(this, ServerSetActivity.class);
startActivityForResult(serverSetIntent, CHANGE_IP);
constants setting and result catching: (I have them together, because only here in catching both costants meet)
static public int CHANGE_IP = 1000;
static public int CHANGE_IP_DONE = 1001;
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// If the request was for CHANGE_IP and the request was CHANGE_IP
if (resultCode == CHANGE_IP_DONE && requestCode == CHANGE_IP) {
readBaseInfoFromServer(getApplicationContext());
startVideoPlayerService(getApplicationContext());
setCurrentChannelAndPlay(getApplicationContext(), 0);
}
}
----------------------------------- ServerSetActivity ----------------------------------------
ending of the called activity
final Intent intent = new Intent();
setResult(PackVideo.CHANGE_IP_DONE, intent);
finish();
#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 (resultCode != RESULT_OK) {
if (requestCode == 2 && data != null) {
//DO YOUR OVER HERE}