How to disable a button from another activity? - java

So, I have a menu activity and there a user can choose a game to play. When he does that, in game he has an option to go back to menu (either on backPress or on my Exit button in game). I need, when that happens, to disable that game button, so the user cannot play it again. How can I do that?
I tried to make an object from Menu class and then disable the button but get error. Also tried:
public void disableButton(){
button.setEnabled(false);
}
and then calling it:
obj.disableButton
But I get stactOverflow errors on line where I created my obj as soon my menu activity starts. How to do this?
EDIT:
In Menu class:
public void disableButton(){
button.setEnabled(false);
}
In Game class:
Menu objMenu = new Menu();
And then in exit onClickListener:
objMenu.disableButton();
EDIT2:
Here's how I call my game activity:
Intent i = new Intent(this, Game.class);
startActivityForResult(i, GAME);
Then in game activity:
Intent resp = new Intent();
resp.putExtra("score", numberOfPoints);
setResult(1, resp);
finish();
And again in Menu class:
final private static int AS = 1;
#Override
protected void onActivityResult(int reqCode, int respCode, Intent i) {
if(respCode == 1) {
switch(reqCode) {
case AS: receivedA = i.getIntExtra("score", receivedA);
button.setEnabled(false);
break;
}
So, this is what I do when game ends, and I disable that button succesfully.

I think the issues is not only in button.setEnabled(false);. As it is hard to say what is the problem without seeing code's details, I am suggesting another solution
start the game activity from menu activity using startActivityForResult
when the game is finished ,in the return intent put a specific data
In Menu activity, in onActivityResult check if the result intent has that specific data
basing on step 3 you can enable or disable the button

Related

Android: how to quit an activity without closing it when click on back button?

I have a subActivity that can be open from my mainActivity.
For some reasons, when the user clicks on the back button go back to my mainActivity, I want my subActivity to remain open in background in order to be able to come back for later.
Questions:
how to avoid to close the subActivity when the user clicks back ?
how to come back to the mainActivity without restarting it ?
how to come back later to my opened activity without re-creating it completely ? (just want to bring it to front)
Thanks !
On you subActivity onBackPressed() add this
#Override
public void onBackPressed() {
Intent i = new Intent(SubActivity.this, MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(i);
}
on mainActivity :
private void openSubActivity() {
Intent intent = new Intent(MainActivity.this,SubActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
}
override onBackPressed() and remove super from it then try opening the activity which you want to open from there for ex-
#Override
public void onBackPressed() {
// your code
}
and with the help of different activity launch modes you can achieve it

How to exit an app when Back Button is pressed

This is a small question but I'm finding very hard to solve this. I have multiple activities. For example A B and C. When app is launched A is opened and on click of button will redirect me to activity B and then from B on click of another button will take me to C. On Back pressed will now bring me back to B and again on Back button is pressed will take me to A which is main Activity.
Now if I press back button, instead the app should exit...it creates loop between B and A and never exit the app.
I already used the following method
Method 1:
use of this.finishonBackPressed which didn't help
Method 2:
use android:nohistory = true in manifest
But If I do so then from C activity it will directly take me to A which I don't want.
Method 2.
Use of
Intent intent = new Intent(Intent.ACTION_MAIN);
//to clear all old opened activities
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
when I use this then everytime this opens up in my device
Please anyone help.
This is my code in Mainactivity now
#Override
public void onBackPressed() {
finish();
super.onBackPressed();
}
But also it don't work and it creates loop between A and B Activity.
Your code (in MainActivity) is incorrect , put finish() after super.onBackPressed()
it must be :
#Override
public void onBackPressed() {
super.onBackPressed();
finishAffinity(); // or finish();
}

Call a Method from another Method Android

I am not sure this workaround is the correct way to achieve my goal of having a prompt text in a spinner. What happens with this application is the spinner navigates to another Activity via an Intent and when the user navigates back to the Main Activity with the spinner they have two ways back. One with a Button and a click event the other by clicking the device BACK button. I am trying to call the code in the click event from the method that manages the device BACK button
I do not know how to call the click event from the device BACK button Method
#Override
public void onBackPressed() {
Toast.makeText(getApplicationContext(),"Use BACK BUTTON\n\n"+"On the Screen",Toast.LENGTH_LONG).show();
// I want to call goBack(View view) from here
// +++++++++++++++++++++++++++++++++++++++++++
}
public void goBack(View view){
Intent i = new Intent( PageTwo.this, MainActivity.class );
startActivity( i );
}
The reason I use this Intent to navigate BACK to the Main Activity is it reloads the variables in the Spinner
It looks like goBack(View) is most likely from an onClick setup in your layout XML. Since you aren't using the view, just pass null:
#Override public void onBackPressed() {
goBack(null);
}
I don't know if I get you right, if you just want to go back to the activity which started another activity, you can just call finish() method of Activity class:
#Override
public void onBackPressed() {
finish();
}
finish() reference

On physical back button pressed how to go to device's home screen? [duplicate]

When a user presses the back button on an intent, the application should quit. How can I ensure the application quits when the back button is pressed?
In my Home Activity I override the "onBackPressed" to:
#Override
public void onBackPressed() {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
so if the user is in the home activity and press back, he goes to the home screen.
I took the code from Going to home screen Programmatically
Immediately after you start a new activity, using startActivity, make sure you call finish() so that the current activity is not stacked behind the new one.
EDIT
With regards to your comment:
What you're suggesting is not particularly how the android app flow usually works, and how the users expect it to work. What you can do if you really want to, is to make sure that every startActivity leading up to that activity, is a startActivityForResult and has an onActivityResult listener that checks for an exit code, and bubbles that back. You can read more about that here. Basically, use setResult before finishing an activity, to set an exit code of your choice, and if your parent activity receives that exit code, you set it in that activity, and finish that one, etc...
A better user experience:
/**
* Back button listener.
* Will close the application if the back button pressed twice.
*/
#Override
public void onBackPressed()
{
if(backButtonCount >= 1)
{
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
else
{
Toast.makeText(this, "Press the back button once again to close the application.", Toast.LENGTH_SHORT).show();
backButtonCount++;
}
}
The app will only exit if there are no activities in the back stack. SO add this line in your manifest android:noHistory="true" to all the activities that you dont want to be back stacked.And then to close the app call the finish() in the OnBackPressed
<activity android:name=".activities.DemoActivity"
android:screenOrientation="portrait"
**android:noHistory="true"**
/>
Why wouldn't the user just hit the home button? Then they can exit your app from any of your activities, not just a specific one.
If you are worried about your application continuing to do something in the background. Make sure to stop it in the relevant onPause and onStop commands (which will get triggered when the user presses Home).
If your issue is that you want the next time the user clicks on your app for it to start back at the beginning, I recommend putting some kind of menu item or UI button on the screen that takes the user back to the starting activity of your app. Like the twitter bird in the official twitter app, etc.
Use onBackPressedmethod
#Override
public void onBackPressed() {
finish();
super.onBackPressed();
}
This will solve your issue.
First of all, Android does not recommend you to do that within the back button, but rather using the lifecycle methods provided. The back button should not destroy the Activity.
Activities are being added to the stack, accessible from the Overview (square button since they introduced the Material design in 5.0) when the back button is pressed on the last remaining Activity from the UI stack. If the user wants to close down your app, they should swipe it off (close it) from the Overview menu.
Your app is responsible to stop any background tasks and jobs you don't want to run, on onPause(), onStop() and onDestroy() lifecycle methods. Please read more about the lifecycles and their proper implementation here: http://developer.android.com/training/basics/activity-lifecycle/stopping.html
But to answer your question, you can do hacks to implement the exact behaviour you want, but as I said, it is not recommended:
#Override
public void onBackPressed() {
// make sure you have this outcommented
// super.onBackPressed();
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
To exit from an Android app, just simply use.
in your Main Activity, or you can use Android manifest file to set
android:noHistory="true"
finish your current_activity using method finish() onBack method of your current_activity
and then add below lines in onDestroy of the current_activity for Removing Force close
#Override
public void onDestroy()
{
android.os.Process.killProcess(android.os.Process.myPid());
super.onDestroy();
}
I modified #Vlad_Spays answer so that the back button acts normally unless it's the last item in the stack, then it prompts the user before exiting the app.
#Override
public void onBackPressed(){
if (isTaskRoot()){
if (backButtonCount >= 1){
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}else{
Toast.makeText(this, "Press the back button once again to close the application.", Toast.LENGTH_SHORT).show();
backButtonCount++;
}
}else{
super.onBackPressed();
}
}
you can simply use this
startActivity(new Intent(this, Splash.class));
moveTaskToBack(true);
The startActivity(new Intent(this, Splash.class)); is the first class that will be lauched when the application starts
moveTaskToBack(true); will minimize your application
Add this code in the activity from where you want to exit from the app on pressing back button:
#Override
public void onBackPressed() {
super.onBackPressed();
exitFromApp();
}
private void exitFromApp() {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
}

Show information based on what button that was pressed

I'm making a information app for Android and I can't figure out how to change a button depending on which button was pressed in a previous class.
I made this picture that shows how the app works:
After clicking the first button you get to a new class with 2 new buttons that sends you to the same class but the 3 buttons there is supposed to change depending on which one of the two buttons you pressed, the three buttons you get sent to sends you to the same class but there is a TextView that changes text depending on what button you pressed.
So I need to add some kind of information to the button (an intent?) so it knows what you pressed earlier.
I'm kinda new to Java and Android so I'm sorry if I explained in a weird way.
Thanks in advance
Each time you start a new Activity, but some extras in that intent telling the next activity what the buttons should display. In the next activity, read the extras from the intent you got, and programatically set your buttons' text accordingly.
Here's some psuedo code.
public class FirstActivity extends Activity{
//setup button 1 and two first, then set their onClickLiseners like so
View.OnClickListener clickListener = new View.OnClickListener(){
public void onClick(View view){
Intent newActivity = new Intent(FirstActivity.this, SecondActivity.class);
if(view == button1){
//add extra to intent indicating button1 was clicked
}
else{
//add extra to intent indicating button2 was clicked
}
startActivity(newActivity);
}
};
// other stuff in your activity
}
public class SecondActivity extends Activity{
protected void onCreate(Bundle icicle){
Intent startedBy = getIntent();
if(started by has the extra indicating button 1 was clicked){
//do button1 stuff
}
else{
//do button2 stuff
}
}
}
}

Categories

Resources