0

I will try to explain the cenario.

Look this image 1:

cenario

I have one Fragment called HOME with a toolbar.

In that Fragment i have a ViewPager with 2 fragments:

Fragment A and Fragment B

When the fragment A is activy, the toolbar has a action button that call another activity.

In that "another activity" i have a ViewPager with 2 fragments: Fragment C and Fragment D.

What i need is, when i click on a button inside Fragment C, the "another activity" closes and execute a callback inside Fragment A.

I tried with startActivityforresult but without sucess.

Tried too wiht a public interface inside fragment C that is implemented by fragment A, but i think this does not work like intended.

Anyone can give me a tip?

And sorry my english, not my native language.

Some code to explain:

Inside Fragment A

When the button inside toolbar is clicked him call "Another activity"

startActivityForResult(IAddPedido, RESULT_ADD);

The "another activity" create the viewpager and setup 2 fragments:

fragManager = getSupportFragmentManager();

adapterPedidosAdd = new AdapterPedidosAdd(fragManager);

// Seto adaptador passando o Id
viewPagerPedAdd.setAdapter(adapterPedidosAdd);

tLayoutPedAdd.post(new Runnable() {

    @Override
    public void run() {

    tLayoutPedAdd.setupWithViewPager(viewPagerPedAdd);

    }

});

Inside fragment C, when i click on "button" him will call set the result and call "finish()".

// Get the "another activity" and set the result
getActivity().setResult(1);

// Close the "another activity"        
getActivity().finish();

And then, inside Fragment A i have the "onActivityResult":

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {

}

But the "onActivityResult" inside Fragment A is never called.

Maybe i have to create "onActivityResult" inside "Home" and then, get the instance of fragment A to call a methode inside him?

PS.: The "Home" is aready an fragment, because i have a NavigationDrawer.

Eduvm
  • 1,131
  • 8
  • 14
  • check out [eventbus](https://github.com/greenrobot/EventBus). – MidasLefko Mar 02 '16 at 18:21
  • You should be able to do this with startactivityforresult. How is it not working? – Eric S. Mar 02 '16 at 18:22
  • I would like to recommend another event bus library http://square.github.io/otto/ . You have an example here https://github.com/square/otto/tree/master/otto-sample/src/main/java/com/squareup/otto/sample – AndroidRuntimeException Mar 02 '16 at 18:27
  • Yes use a lib or.... you could just write up a simple method here is an example http://stackoverflow.com/questions/13700798/basic-communication-between-two-fragments – Xjasz Mar 02 '16 at 18:29
  • Check this [answer](http://stackoverflow.com/a/33756734/4440874). – Jyotman Singh Mar 02 '16 at 18:50

1 Answers1

0

getActivity.setResult() is not work in your code because of life cycle of fragments and activity.

this code shouldn't be in onDestroy(fragment). onDestroy happens after the activity is already finished, and onActivityResult was called.

instead of that

this code needs to in the code that closes the activity/fragment, like on back key pressed, or a close button onClick

I tested your case scenario in my sample project and it's working

Attiq ur Rehman
  • 475
  • 1
  • 6
  • 21
  • I have edited my answer, try to set Result in your another activity using an interface from fragment C/D – Attiq ur Rehman Mar 02 '16 at 19:52
  • So, tried putting getActivity.setResult(1) inside onDestroy (inside Fragment C) but doesn't work. The onActivityResult isn't called Sorry, I didn't see your comment. Will, try using the interface – Eduvm Mar 02 '16 at 20:25
  • look the suggested part of answer (2nd yellow paragraph) – Attiq ur Rehman Mar 02 '16 at 20:31
  • Lemme know if you could solve the problem, otherwise I will share the dropbox link with you. It's 1:35 AM in my side so it's time to sleep – Attiq ur Rehman Mar 02 '16 at 20:35
  • Sorry. I haven't been able to solve. Can you share your code with me, please? – Eduvm Mar 02 '16 at 22:56
  • Solved the problem with de EvenBus library. But, anyway, thank you for you attention and pacience. – Eduvm Mar 03 '16 at 11:49