Reset Entire Activity in Eclipse - java

I need help to reset an entire activity after i am done with it, so that when I comeback to it, everything is back to it's original setting.
So, I have an activity named ColorGame and Scoreboard. What I want to happen is to after I finish playing in ColorGame Activity, it will go to Scoreboard (which is working fine). Then in Scoreboard Activity, I have a button that either will go to the main menu or to go back and play again in ColorGame Activity. I have an intent from Scoreboard to the ColorGame activity, however, everytime I click the PLAYAGAIN button to return to play the game again, the points and time is still the same number from the previous game. Could you guys help me?
I have these codes. This is my code for ColorGame to ScoreBoard:
Intent goScoreBoard = new Intent(ColorGame.this, ScoreBoard.class);
goScoreBoard.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
finish();
startActivity(goScoreBoard);
and this is my codes for ScoreBoard to ColorGame:
Intent playAgain = new Intent(ScoreBoard.this, ColorGame.class);
playAgain.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
finish();
startActivity(playAgain);

It seems that you're using single top launch mode for the Activity. You should implement onNewIntent in ColorGame and reset all the data in the Activity there.

Related

Prevent an android activity to go pause state when HOME button is pressed

While using an editor activity I will prevent a HOME button press. But...
I have tried Key events catching up on activity nothing hits there. also tried onUserLeaveHint() to show a confirmation dialog.
I started activity like following
final Intent intent = new Intent(activity, A.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_USER_ACTION | Intent.FLAG_ACTIVITY_NEW_TASK);
activity.startActivityForResult(intent, "EDITOR");
any idea what could be a good practice to achieve this?
I don't think its possible to do, you cannot override Home button press and hence you cannot prevent your activity to go to in pause state.
actually it goes to stop state when you click on home. first pause and then stop.

How to display text in a new activity when two radio buttons from two different activities are clicked?

I am using Java and Android Studio. I have one activity with a few options to select from, and once one of them is selected, they click the button that takes them to the next activity. They do the same thing again except this time when the user clicks the button it takes them to a new activity where it displays the results based on the two radio buttons clicked previously. How can I do this?
You can send some variable through intent and onCreate() method of the second activity, you can use that variable to differentiate your functionality.
This video demonstrates the functionality of the radio button: https://youtu.be/zJG9Prn3vZ0
In this video you can know how to pass arguments to another activity: https://youtu.be/OMCctaE66b8
Hope that helps you to solve your problem.
You cannot declare a method inside another method. Put a closing brace after onCreate.
When you are opening a new activity, you are doing it with an Intent. Put the values you want to send (bool clicked) the new activity inside the intent

How to navigate back to my app instead of going down the backstack?

Let's say the user presses a button which causes another activity to get launched. Now the user is in another activity. What flag do I have to add to the intent, so that the user returns to my app when pressing the back button instead of navigating down the back stack of the started activity?
From the documentation, "activities on the backstack are never rearranged."
Android documentation Tasks and BackStack
You do not have to add anything to the intent. When you navigate out of you app and start another activity to complete a task, the new activity becomes kind of like an extension of your app. Pressing the back button takes you back to your app. Read This

Exit from Application android

I tried some way to exit from application but each method just minimizes the app. I'd like to close down the app not minimize it. Exit Button is in MainAcitivity.
For now i'm calling this function, it works but not as i need.
public void AppExit()
{
this.finish();
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
I know this question is asked a lot but i can't find solution exactly what i need.
After your advice i use this function
public void AppExit()
{
System.exit(0);
finish();
}
If you want to exit why you are starting activity again.
use only this.finish(), remove other statements.
Take look at activity life cycle for more info
Few things. In your code you can put finish(); after startActivity, this may help (did for me).
How it should be done however is the way the guidelines have it already. The back button does close the app. If you press the back button and then restart your app, you will be on the very first page activity, not where you left off.
In regards to the back button, you can Override it to control what it does if you are not happy with it. You can put your code in there and when the users presses the back button it will execute the code. Users are more accustomed to pressing the back button rather than a button within a layout (which I presume is what you may be doing).
This might be helpful if you are sticking to what you have: How to clear the Android Stack of activities?
Try with this single line code :
System.exit(0);
You need to navigate to your main activity with Intent.FLAG_ACTIVITY_CLEAR_TOP, pass it some data that it will know that you are exiting the application. And just call finish from your main activity.
Another suggestion is Kamlesh Arya answer. How ever it is a bad practice as it will not destroy the activity stack, and next time the use lunch your application it will not start from the main activity but from the last active activity.

Android: launchMode=SingleTask problem

I have an app that circles around the main activity (a main menu). In each other app there is an option menu item that directs to this activity.
At first, I always started a new main activity when this item was selected. Using the intent bundle, I did tell the main activity that some initializations I do on a fresh start were not necessary.
However, I didn't quite like the overall behavior. I stumbled upon android:launchMode="SingleTask" and this seemed to help: now I don't recreate my main menu activity all the time; also, if I press the "back" button I come back straight to the home screen. This feels quite nicely like a proper "main" menu.
My problem now is this: if I run another activity of my app, press home button and then reopen my app (e.g. using "last apps"), then I don't go back to the last activity, but to the main one. The other activity is destroyed.
Any ideas how I can implement the behavior of SingleTask without only being able to return to one activity?
If your other activities are declared normally with activity defaults in Android, then going back to your app should take you to the same activity where you left off (using the hardware home button)
However remember that the Android system kills applications when it requires system resources. So your app may have been killed when you went to the other application. Then when you get back to your app, the default launcher activity will be restarted, which is your Menu activity.
To get back to the main activity from any activity, do this:
public static void goHome(Context context) {
final Intent intent = new Intent(context, HomeActivity.class); //give name of your main activity class here
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(intent);
}
That will clear the activity stack and get you back to your main activity. As you declared singleTop, it will bring the existing main activity to the foreground. The flag Intent.FLAG_ACTIVITY_CLEAR_TOP will remove all activities in the stack on top of the main activity. (I am assuming you are within the same application).
Now, all your other activities only need to include a button whose click listener invokes the method goHome();
From your main activity, if you press the hardware back button, it should exit your app.
Why not call finish() on the activities that were created by the main activity? This way you return to the main activity, without creating a new one...
I think you should save the state of you activity before starting another activity, and then resume your activity whenever you come back on last activity.
see Activity Life cycle from Android
http://developer.android.com/guide/topics/fundamentals/activities.html

Categories