Is there a way to change what a Button does in another Activity from a Fragment? For example. I have a Fragment lets call it Fragment A. I have a game and when you win it changes the screen to the win screen Activity. There are two Button on that Activity, Next level and main menu. So for example if you're on level one it opens the level one win screen upon winning. Which as mentioned previously has two Buttons. The next level button takes you to level 2. If you win level 2 it pulls up the level two win screen which is the exact same as the level 1 win screen other than when you press next level it takes you to level 3 not level 2. The way you lose in this game is you click a Button. To summarize my question is there a more efficient way to do this other than make an win Activity for every single level that is the exact same as the last other than the next level Button loads the current level +1 If there is not another way to do this I would be utterly shocked because lets say I have 10 levels. Each level has to have its own win screen and lose screen which means for 10 levels i'm creating 10 fragments and 20 activities. Each level has a level screen, a win screen and a lose screen. Which seems extremely inefficient. I want to reuse the same exact win screen for each level and be able to just change where the next level Button takes you with out having to make 10 different win screens just to have that one difference met.
Asked
Active
Viewed 65 times
0
-
overriding the method that is called, conditional statements, ... – Stultuske Oct 27 '17 at 06:20
-
Do you have any resources I could look at that show me how to change what a button does in a activity when a button in a fragment is pressed? – Marcus Cantu Oct 27 '17 at 06:23
-
You can pass the currentLevel as a bundle argument to your fragment and whenever the next button is clicked you can just take the player to curentLevel+1. If you can post some code, that'll be much helpful to see what else can be done. – Amit Barjatya Oct 27 '17 at 06:26
-
@Cantum2 what I mentioned was really the basics of the basics. if you don't know how that works, you should learn that before trying to work with activities – Stultuske Oct 27 '17 at 06:27
-
I mean I understand over riding a method and conditional statements. As @AmitBarjatya said I did not know about bundle arguments. – Marcus Cantu Oct 27 '17 at 06:29
-
@Cantum2 [this](https://stackoverflow.com/questions/9245408/best-practice-for-instantiating-a-new-android-fragment) might help – Amit Barjatya Oct 27 '17 at 06:32
-
Awesome thank you so much! – Marcus Cantu Oct 27 '17 at 06:53
1 Answers
0
For those of you who are looking for a solution to this question here is what I did. I created a bundle similar to the code below in my fragment in my Fragment class I did not put it any of the life cycle methods but I did put it inside an if statement:
Intent i = new Intent(getActivity(), Java.class);
Bundle bundle = new Bundle();
bundle.putInt("Key", 1);
i.putExtras(bundle);
startActivity(i);
From there I retrieved the bundle in my activity in my onCreate Method as follows:
Bundle bundle = getIntent().getExtras();
int EnterYourInt = bundle.getInt("Key");
Then I used an if statement to check and see which number my key was then holding to load the next level. Thanks to Amit Barjatya for linking me. Also this video was extremely helpful https://www.youtube.com/watch?v=YxffGKnS74Q.

Marcus Cantu
- 463
- 3
- 14