Sending value to activity through another - java

I am working on an update for my Rock Paper Scissors game (I know it's a simple concept and has been done before but still), and the update includes massive changes in layouts and the programming.
So far I have Three working Activities; MainMenu, SinglePlayer, SettingsActivity
The way that things work currently is the user will change a setting (Background, Vibration, etc) inside the SettingsActivity, and when the user hits the Main Menu button it will bundle all values (float backgroundNumber, boolean Vibration) and will send it over to the MainMenu activity. The MainMenu will then unpackage the values and save them, so when the user opens SinglePlayer or any other activity it can send the appropriate values over to that activity.
The issue comes in when the user wants to reset the SinglePlayer score counter...
Here's what works: User presses "Reset Score" inside Settings -> User presses Main Menu button -> User open Single Player (Score resets and everything works fine)
Here's what doesn't work: User presses "Reset Score" inside Settings -> User presses Main Menu button -> User opens a different activity other that Single Player/exits app -> User goes back to main menu -> User goes into Single Player (Score does not reset)
The Main Menu saves all values other than the resetScore boolean (So the score counter doesn't get fed 'resetScore = true;' every time SinglePlayer gets opened).
I'm not trying to advertise but if you are having problems following my description, try following the steps provided above live with the app: https://play.google.com/apps/testing/com.simplegames.chris.rockpaperscissors20

This may be considered a "hack", but this helped me.
After pressing the "Reset Score" button the SinglePlayer Activity is immediately opened to reset the score and then sent back to the Settings Activity. It happens so quickly that it doesn't even look like it left the Settings Activity.
I would remove the question, but who knows... someone else (or me in the future) might need it?

Related

New Activity everyday by clicking a Button

I want to make that when user clicks the Start Button, the app shows ActivityFirst (on the first day when he downloaded app), the second day when s/he taps Start - ActivitySecond will be shown etc. When the app will not have any other Activity, I want to show a popup that will say that more Activities will be added in the future.
I can do popups, Activities, Buttons, onClicks etc.
But I can't do that everyday will there be a new Activity.
Can you help me, guys?
My idea:
The user downloads the app,
The user runs the app for the first time and clicks the Start Button (here comes the ActivityFirst)
The user can look at this Activity for one day
The user runs the app on the second day and taps the Start Button (here comes the ActivitySecond)
And so on.
I hope you understand my idea, it's a very annoying problem to me.
What I would do, is save the timestamp of the first launch of the app.
Then, every time the user opens the activity, I would check how many days it's been since that timestamp, and populate the Activity accordingly.
All you need is one "DailyActivity", really.

How to stop app from restarting when user presses the back button

I am working on a small app. When the user presses the app icon it starts activity A, which in turns starts activity B. Activity A then completes. B is set up as a main menu and can start other activities: the user can navigate back to B with the back button.
If the user navigates back to B and presses the back button the app moves into the background and the user is at their home screen. I have not overridden anything; this is the normal navigation.
At this point, if the user presses the app icon the app restarts. I understand that when the app is in the background the OS can close it for memory purposes, but this happens every time - regardless of how much memory. Is there a way to change this behavior? I already figured out how to stop this action with the home button with:
if (!isTaskRoot())
But I need to stop the action on the back button.
You can override onBackPressed() of Activity B like this,
#Override
public void onBackPressed () {
moveTaskToBack(true);
}
The app will be hidden when the user presses the back button, but it's state will remain the same. And when it is reopened, it will appear just as it was when you left it.

Android task: click a button, go to a layout to edit personal info. click the submit button, save and display the info

I am working on a small task for an android app. And I dont really know what are the things I need to do or things I should be careful for.
In a user's personal page, there is a button where the user can click in. Then it goes to a layout where there are two buttons "cancel" and "submit". Below them, there is a blank space where the user can edit his personal info. (Here the keyboard should automatically pop up).
After he clicks "submit", the info is saved and the app goes back to the personal page and the info he just entered will be displayed.
If a user already has some text for his info, after he clicks the edit button, the whitebox in the layout should has the info that he has previously entered. from that he can re-edit and submit again.
Any type of hints and guidelines will be greatly appreciated!! Thank you so much!!!
This sounds like a good place to use Activity results. You can make the screen where the user enters the information an Activity, and then start it by calling Activity.startActivityForResult(). Once the user is finished and clicks done, you store the result with Activity.setResult() and call finish() to close that activity. The original Activity that opened that screen will then get a callback in onActivityResult() with the data that you set in setResult(). This is a pretty common task so there should be plenty of examples around.

How to set play to a JButton one sound at a time in java?

I have been working on a project for few weeks now. This is the Main Basic UI for the Application
When User Press a button A relevant .wav file is playing but it wont't stop playing when user click another button. i want it to be stopped when user click another button.
You will have to make the current play to stop before another click ,i.e a play request comes in.
public void playClip(String clipName){
if(audioPlayer.isPlaying()){
audioPlayer.stop();
}else{
audioPlayer.setClip(clipName);
audioPlayer.play();
}// not actual code, but you get the idea

PreferenceActivity, how to make a Preference item that will navigate back through menus?

My overriding question is this: In Android PreferenceActivity, how can I write an OnClickListener which will have the same functionality as pressing the Android back button as I navigate through PreferenceScreen defined menus? That is to say, I would like users of my App to explicity see a menu choice "Back" which will bring them to the previous menu, or bring them out of the menu activity to their previous activity if they are at the root of this particular PreferenceActivity session.
The android developer documents tell us
Note that this XML resource contains a preference screen holding another fragment, the Prefs1FragmentInner implemented here. This allows the user to traverse down a hierarchy of preferences; pressing back will pop each fragment off the stack to return to the previous preferences.
And they are correct about that. I navigate happily through my menus by clicking on PreferenceScreen items to get to that screen, and using the Android back button to go Back up a level. But I'm not sure a casual user really understands the "Back" button, I know I didn't until I read about it in Developer docs. SO I would would like them to have an explicit Preference defined menu choice whos OnClickListener duplicates the function of the Android back button.
So I tried to put in a Preference in my menu that would go back. Having determined that a not Overriden onBackPressed in a my subclass of PreferenceActivity just referred back to Activity.onBackPressed() which merely calls finish(), I tried this OnClickListener:
private OnPreferenceClickListener clickFinishesSuccessfully = new OnPreferenceClickListener() {
#Override
public boolean onPreferenceClick(Preference preference) {
finish();
return true;
}
};
As it turns out, this did NOT do the same thing as pressing the back button! Pressing this button always took me out of the PreferenceActivity entirely, back to the Activity from which I had called my PreferenceActivity. Specifically, it did NOT navigate back through my menus no matter how deep I was when I clicked it.
I am guessing here: When I have gotten to a submenu by clicking an onscreen preference which is really a PreferenceScreen, I am no longer in my own PreferenceActivity. I must be in some other Activity?
So my functional question: what can I put in my OnClickListener of my "Back" Preference to get the same function as the Android Back button navigating through my menus?
I think the casual user should know about the back button. The button is used everywhere so it might be a problem getting used in the first day but after that it's natural. Without being used to the "back" button I can hardly imagine doing the everyday tasks.
The preference you want to add just duplicates functionality and doesn't provide a consistent way with the rest of the system. If Google was considering back being an uncommon thing for casual users would have added that option in phone's Settings which is also a PreferenceActivity.

Categories