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?
Related
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
}
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.
i have this fragment which makes a call to an activity using start activity for result. this activity is supposed to show a lock pattern. the problem is that the 'onActivityResult' is never called. I put some toasts to check but it never gets printed.
public class int_Create_Pattern extends Fragment {
private static final int REQ_CREATE_PATTERN = 1;
#Override
public void onStart() {
// TODO Auto-generated method stub
super.onStart();
LockPatternView.MATRIX_WIDTH = 4;
Intent intent = new Intent(LockPatternActivity.ACTION_CREATE_PATTERN,
null, getActivity().getBaseContext(), LockPatternActivity.class);
startActivityForResult(intent, REQ_CREATE_PATTERN);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(resultCode, resultCode, data);
switch (requestCode) {
case REQ_CREATE_PATTERN: {
if (resultCode == LockPatternActivity.RESULT_OK) {
char[] pattern = data
.getCharArrayExtra(LockPatternActivity.EXTRA_PATTERN);
DataBaseHandler handler = new DataBaseHandler(getActivity()
.getApplicationContext());
handler.open();
String PatternToWrite = new String(pattern);
handler.createPattern(PatternToWrite);
handler.close();
Log.d("DEBUG", new String(pattern));
Toast.makeText(getActivity().getApplicationContext(),
"Pattern Recorded", Toast.LENGTH_LONG).show();
}
if (resultCode == LockPatternActivity.RESULT_CANCELED) {
Toast.makeText(getActivity().getApplicationContext(),
"Pattern Cancelled", Toast.LENGTH_LONG).show();
}
break;
}// REQ_CREATE_PATTERN
}
}
}
onActivityResult should be an Activity method, and not be on a Fragment.
Another problem is that you shouldn't call a new Activity from a Fragment. Implement an Interface for comunication, as described by the link bellow
http://developer.android.com/training/basics/fragments/communicating.html
The activity that holds your fragment should be the responsable for calling the new activity, and processing the result.
In Parent Class: do this code
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.dualPane);
fragment.onActivityResult(requestCode, resultCode, data);
}
In Child Class:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//in fragment class callback
}
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
}
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);
}
}