• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

startActivityForResult doesn't work in a Fragment

 
Alex Rome
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm using an Activity with DrawerLayout (NavigationDrawer) that show a Fragment and this fragment has three EditText and a ImageView, if I click on ImageView it should open the image picker but nothing happens.

This is the code:



P.S: if I use the same code in a Activity class, the picker opens, but I need that it opens from a Fragment
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That may not be possible - what's the issue with having the associated activity do it instead?
 
Alex Rome
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:That may not be possible - what's the issue with having the associated activity do it instead?
I'm probably wrong understanding but I need to use a Fragment and not an Activity because I'm using the DrawerLayout in the MainActivity
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wasn't suggesting that you don't use fragments, I'm suggesting that you do perform this action in the activity instead of the fragment.
 
Alex Rome
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:I wasn't suggesting that you don't use fragments, I'm suggesting that you do perform this action in the activity instead of the fragment.
how can I do this? My view is inside the fragment
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My favorite approach for communication between various components of an app is the EventBus library.

Or you can cast getActivity() to whatever class it actually is, and then make a direct method call.
 
Alex Rome
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:My favorite approach for communication between various components of an app is the EventBus library.

Or you can cast getActivity() to whatever class it actually is, and then make a direct method call.
I used this code in the MainActivity but nothing happens if I click the ImageView:

 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm confused - earlier you said the code worked fine when part of an activity. But now it doesn't?

The first thing to investigate whenever an event handler seems to have no effect is to find out if it is ever actually called.
 
Alex Rome
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:I'm confused - earlier you said the code worked fine when part of an activity. But now it doesn't?

The first thing to investigate whenever an event handler seems to have no effect is to find out if it is ever actually called.
If I use all the code of the Fragment as Activity, the action works, instead if I use only the piece of code about the action, it doesn't work.

in this way works fine, but I need the same result with a fragment:

 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I use only the piece of code about the action, it doesn't work.

That was my earlier question: how does it not work? What did you do, what did you expect to happen as a result, and what happened instead? What concrete steps have you taken so far to investigate the issue?
 
Alex Rome
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:
I use only the piece of code about the action, it doesn't work.

That was my earlier question: how does it not work? What did you do, what did you expect to happen as a result, and what happened instead? What concrete steps have you taken so far to investigate the issue?
The issue is that I haven't result because the image picker doesn't open from the fragment, if I click/touch the ImageView, nothing happens, instead with the same code in the Activity, the image picker opens and I can select an image from the gallery, so in this way I have the result. Using the fragment and reading the logcat, there isn't anything about the issue...It's a mistery
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
because the image picker doesn't open from the fragment, if I click/touch the ImageView, nothing happens

That, too, goes back to something I said earlier:
The first thing to investigate whenever an event handler seems to have no effect is to find out if it is ever actually called.

Have you ascertained that the event handler code is actually executed?
 
Alex Rome
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:Have you ascertained that the event handler code is actually executed?
I inserted a Log in the onClick method and I found that the event handler insn't called, but why? The code seems ok
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Setting 2 event handlers (in lines 19 and 20) can't be right.
 
Alex Rome
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:Setting 2 event handlers (in lines 19 and 20) can't be right.
I removed one of the event handlers but I have the same problem, also the issue is on avatar ImageView on line 47, not on the button
 
Alex Rome
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I solved the issue, in the MainActivity I used a static Fragment with very similiar name of my Fragment that I posted. FACEPALM
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic