1

Is there a way to send a fragment to an activity? Or is there a better implementation method for my usage?

I have a gameOneFrag and gameTwoFrag.

For example: From a fragment i want to open one of them in an activity (depending on which button was pressed inside the fragment). I could do a FragmentTransaction and replace current fragment with any of the game fragments. But i would rather like to open a new activity "GameActivity" with an intent, and send one of the gameFragments as an extra or something.

I was thinking of something like this:

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            //listView.getItemAtPosition(position).getFrag() instead of ;
            Intent intent = new Intent(getActivity(), GameActivity.class);
            intent.putExtra("GAME_TYPE", gameOneFrag );
            startActivity(intent);
        }

Than in GameActivityFragment

    Fragment frag = getIntent().getExtras().getFragment("GAME_TYPE");
    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(ff.getId(), frag)
                .commit();
    }

Or do i really have to make each game an activity and skip the GameActivity "container" alltogether?

Worth to mention is that i have a Game class where i am planning to store the fragment. For example

Game one = new Game(new GameOneFragment());

Than i can later call one.getFragment();

user3711421
  • 1,658
  • 3
  • 20
  • 37
  • Oh yeah, I did that once. But it wasn't pretty. http://stackoverflow.com/q/24081101/2413303 You're honestly better off if you communicate towards the Activity using `Otto` as per my answer here http://stackoverflow.com/a/28480952/2413303 – EpicPandaForce Mar 11 '15 at 22:03
  • Will take a look a that tmr, seems like new stuff for me :) – user3711421 Mar 11 '15 at 22:34

1 Answers1

1

You can't pass a Fragment as an Intent extra. Instead, you can pass a flag. In your first Activity, define some constants.

public static final int FRAG_ONE = 0;
public static final int FRAG_TWO = 1;

When you have figured which Fragment you would like the second Activity to open, send a constant through the Intent.

intent.putExtra("GAME_TYPE", FRAG_ONE);

In your second Activity, you can retrieve the Intent extras.

int flag = getIntent().getIntExtra("GAME_TYPE", 0);

Now you know which Fragment to load. A switch statement is perfect for this.

switch (flag) {

    case ActivityOne.FRAG_ONE:

        loadFragmentOne();

        break;

    case ActivityOne.FRAG_TWO:

        loadFragmentTwo();

        break;

    default:

        Log.e(getClass().getName(), "Could not load Fragment");


}

In my opinion, I would suggest just replacing the container with a game Fragment as opposed to starting a new Activity although you said you would prefer not to.

Good luck and happy coding!

John P.
  • 4,358
  • 4
  • 35
  • 47
  • Back in the day, I passed a class as a parcelable once which was a Fragment factory. It was weird. – EpicPandaForce Mar 11 '15 at 22:08
  • @EpicPandaForce Wow! I didn't know that was even possible. I much prefer using flags. This way, only primitive data is passed through along with the `Intent`. The flag can be read in the activity's onCreate() and the `Fragment` can be added there. – John P. Mar 11 '15 at 22:12
  • It was possible, but it wasn't pretty. I was sending it as a `Parcelable` through the local broadcast manager, after all. I'd rather use `Otto` now, it's so much simpler :) – EpicPandaForce Mar 11 '15 at 22:14
  • I just tried to make my Fragment Parcelable hehe. Than i tried to send the obj containing the fragment with Gson, but apparently i cannot send an object containing a fragment, had try though :P... Quite clean solution with flags, i was actually leaning towards something similar but would hope for something which would leave out the hardcoding switch statement, one plus for the cleanness of the solution though. – user3711421 Mar 11 '15 at 22:29
  • The opening fragments is in a viewpager with sliding tabs, therefore replacing the container would make things complicated. I could make the gameOneFragment an activity though, than store that activity in my game object.. But is it really good to store either a fragment or an activity in an object? – user3711421 Mar 11 '15 at 22:33
  • @user3711421 You can nest the Fragments if you'd like. In the `ViewPager` `Fragment`, you can add a child `Fragment` by using the fragment's child `FragmentManager`. http://developer.android.com/reference/android/support/v4/app/Fragment.html#getChildFragmentManager() Otherwise, I would say that storing a `Fragment` or `Activity` in an object is not good practice. – John P. Mar 11 '15 at 22:40
  • Yes, thats what i tried but the sliding tabs will be persistent in the gameOneFragment than (which is not wanted), so i would have to mess with slidingtabs in the gameOneFragments. and i would like my gameOneFragment to be as indipendent as possible.. I ended up with your flags solution. Thansk for clarifying that, i think it would make thinks difficult later, i.e. saveing the obj to Shared Press or a database, – user3711421 Mar 11 '15 at 22:47
  • So everytime i add a new gameFragment i will need to create a key for it and add it to the switch statement, right? – user3711421 Mar 11 '15 at 22:54
  • @user3711421 Yes. One flag per `Fragment`. – John P. Mar 11 '15 at 22:57
  • Oh, and how about making the gameOne/Two fragments to activities? Is that the standard way? The only difference would a bit more code in the manifest. What i mean to ask is if it is better to have small stand alone games as fragments or activities. – user3711421 Mar 11 '15 at 22:59
  • @user3711421 That comes down to personal preference. If you feel that you are more comfortable with activities then use activities. Fragments are excellent to use as modular components to an `Activity`. If you are only going to have one game shown at a time in any given situation, I see nothing wrong with using multiple activities; however, fragments can offer more in terms of versatility if you ever decide to have multiple components shown on the screen at the same time. Check out the Android developer post: http://developer.android.com/training/basics/fragments/index.html – John P. Mar 11 '15 at 23:24
  • Sending Fragment as Parcelable is bad idea, I sent a `FragmentCreator` object which had a method that created a new Fragment. But as I said: don't do it! XD – EpicPandaForce Mar 12 '15 at 06:32