android onActivityResult not comming to android.app.Fragment - android

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
}

Related

onActivityResult not called. neither parent's nor nested fragment's

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?

TabbarActivity fragment not called to onActivityResult

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
}

Facebook Login Alternative Solution to setFragment

MainActivity.java
LoginFragment _loginFragment = new LoginFragment();
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
_loginFragment.onActivityResult(requestCode, resultCode, data);
}
LoginFragent.java
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
callbackManager.onActivityResult(requestCode, resultCode, data);
}
At the top I have 1 main activity and a fragment to control my Facebook Login, it turn out that after pressing the button the apps crash on this two class.. How to solve this issues as the use of
android.support.v4.app.fragemt
is not an option for me.

startActivityForResult From Fragment Not Working

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);
}
}

onActivityResult not called in fragment

On my fragment I am doing:
Intent intent = new Intent(getActivity(), OtherActivity.class);
startActivityForResult(intent, RETURN_CODE);
I have overriden onActivityResult on my Activity
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
logger.debug("!!ACTIVITY!!!Returned from the activity!!!!");
super.onActivityResult(requestCode, resultCode, data);
}
The same goes for the fragment! The problem is that this method is only being called on the Activity and not in the fragment. Any clue why?
there is an issue with support v4, try using onPostResume
in onActivityResult in Activity use following code
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
returningWithResult = true;
this.mData = data;
mRequestCode = requestCode;
mResultCode = resultCode;
}
and in onPostResume
#Override
protected void onPostResume() {
// TODO Auto-generated method stub
super.onPostResume();
if (returningWithResult)
<your fragment>.actionOnActivityResult(mRequestCode, mResultCode, mData);
returningWithResult = false;
}
also trying launching intent for result from activity only
public void launchIntent(Intent intent, int code) {
startActivityForResult(intent, code);
}
In Fragment use
getActivity().launchIntent(intent, PICK_FROM_CAMERA);
I dont know the reason, but it worked for me. Hope it helps.

Categories

Resources