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();