0

I have an activity that have some buttons and some fragments.

If I click on button A, i'll show Fragment "FragA". When I'm in "FragA", I can perform some actions like choose a picture from gallery and I need to stay in "FragA" after choose picture.

But when I choose picture, I return to Activity and "FragA" is hidden.

How can perform an action and still in same Fragment or display correct fragment in Activity?

Zong
  • 6,160
  • 5
  • 32
  • 46

1 Answers1

0

You can recreate fragment again and replace it in your Activity with using modification of this code:

if (currentState == STATE_MAIN_FRAGMENT) {
        return;
}
mainScreenFragment = (MainScreenFragment) getSupportFragmentManager().findFragmentByTag(MainScreenFragment.TAG);
if (mainScreenFragment == null) {
    mainScreenFragment = new MainScreenFragment();
}
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.flFragmentContainer, mainScreenFragment, MainScreenFragment.TAG);
fragmentTransaction.commit();

First "if" checks if the fragment is set or not. It is not required but it's a good practice. It prevents you from replacing fragment when it is not necessary.

And there is one thing strange for me. Because you said <<"FragA" is hidden>> - that means it was already set but container is not visible? Then yourFragmentContainer.setVisiblity(View.VISIBLE); in on Activity result.

And the last thing that could help you is to retain the fragment so it won't be ever destroyed and recreated again. Some helpful links:

Understanding Fragment's setRetainInstance(boolean)

http://developer.android.com/reference/android/app/Fragment.html#setRetainInstance(boolean)


Or you can just copy-paste what is in your Button's OnClickListener so it happens onActivityResult too.

Community
  • 1
  • 1
F1sher
  • 7,140
  • 11
  • 48
  • 85
  • Nice! I've did something close you! I've created an int called 'ACTIVE_FRAGMENT' and put value when i click buttons, after i check it inside onResumeFragments(). Thanks, buddy! –  Oct 25 '13 at 15:39