Android Back Button deleting activity for good - java

I am making a game, and when a level is complete I have a pop-up activity which tells you your score.
When I press the back button I want this instance of the pop-up activity to be deleted for good.
Currently I get the behaviour;
Finish level, pop-up appears
I press back button to get rid of it
Start a new level, finish it and new pop-up appears
If I press the back button twice, the new pop-up disappears and the old one re-appears.
I want to delete the old pop-up as soon as it is hidden.
I am using the following code in my pop-up activity;
#Override
public void onBackPressed() {
finish();
super.onBackPressed();
}
Any help would be much appreciated.
Thanks!

You can use this code. It clears off the PopupActivity form the Top Activity Stack so that it will not appear again.
#Override
public void onBackPressed() {
Intent removeIntent = new Intent(GameActivity.this, PopUpActivity.class);
removeIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(removeIntent);
}

Related

disable focus when activity starts

I am working on an application that have a login activity with two edit text and a button. when activity starts first edit text is focused and cursor is blinking. I want to stop this and tried to focus on button with this code in onstart,onresume,oncreate but did not worked:
#Override
protected void onStart() {
btn_login.requestFocus();
super.onStart();
}
What can I do?
Personally I would move the code to onCreate() and then try something like this:
btn_login.setFocusable(true);
btn_login.setFocusableInTouchMode(true);
btn_login.requestFocus();
You will also want to make sure that the EditText is not forcing the keyboard to open with this line:
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

Pause Android app-flow

When a return button is pressed in the app I am working on:
public void clickReturn(View view)
{
// ...
// Call an event handler before finishing the activity
_appData.CustomDetail.hookBeforeFinish(this, new ArrayList<CompSubmission>(_submissions));
// Finish this activity
finish();
}
I am not able to modify clickReturn() - the way the application is structured, all I have access to is the hook hookBeforeFinish().
What I want to do is add a dialog to prompt for some input. I can do this in hookBeforeFinish(), but it only appears for a split second. I assume what is happening is I set up the AlertDialog builder, call builder.show() but finish() is being called directly afterwards - so the dialog only appears momentarily.
Is there anything I can put after builder.show() in the hook function such that it won't continue execution to finish()? If I can pause the application flow until someone presses the OK button on the dialog thatwould work better for me.
Thanks
Please comment finish().
Then read and add a dialog as explained here. In your positive button click (e.g. OK button), add finish();
Edit
Since you cannot override clickReturn() or change the class, you can use your hookBeforeFinish() to create a dialog and comment 'clickReturn();' line.
In your dialog, in the listener for the button which expects to finish the activity add clickReturn();.
This way you will pause the activity closing until user decides to do so.

How Do I Play Sound from another java activity?

As we know the main activity in Android, Eclipse is called MainActivity.java
Actually I have two activites, the second one is: Page2Activity.java
And I have a Page2.xml too for the layout.
I would like to know how can I switch to Page2Activity.java when pushing a button? Because only Page2.xml shows up, and when I click on a button to play a sound nothing happens on the second page.
MainActivity.java
...
bpage2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
setContentView(R.layout.page2);
}
});
...
When I push this button, page2.xml shows up, but it contains sound from Page2Activity.java and when I hit a button the sounds won't play. Could you please tell me how can I load Page2Activity.java with layout page 2?
Regards,
Henrik
You have to fire an event on click action for the button and start page2 Activity on it's listener.
bpage2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, Page2Activity.class);
startActivty(i);
});
Why don't you start the other activity Page2Activity when you click on yout bpage2-Button?
Put this in your listener instead of setContentView(R.layout.page2); :
Intent intent = new Intent(this, Page2Activity.class);
startActivity(intent);
Calling setContentView() sets the layout of your activity - that's why you see the other layout after clicking the button. So what you do is just switching the layout but the logic (e.g. attached listeners) is missing and needs to be implemented. That's why the sound doesn't play. The layout coexists with the logic but it's independant from the logic. Including a xml-layout doesn't mean you include the logic too.
Check out the Fragments http://developer.android.com/guide/components/fragments.html.
In addition take a look on this introduction on how to start an activity from another one: http://developer.android.com/training/basics/firstapp/starting-activity.html .
Maybe you should even start here http://developer.android.com/guide/components/activities.html :)
Good luck.

Prevent back button after logout in android

After logout the user is directed to login screen in android. Now, if user clicks on back button of phone it should stay on login screen itself.
How can I make it possible in android?
I have used following code in my application but it will close my application. It should stay on the login screen only
Intent objsignOut = new Intent(getBaseContext(),Hello.class);
objsignOut.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(objsignOut);
Please guide me the correct way.
override the onBackPressed in your login activity, to do nothing..
public void onBackPressed() {
//do nothing
}
It seems to me that there are simpler and cleaner solutions than overriding onBackPressed method, as mentioned here and here.
You can provide flags when launching a new activity (on login or logout) to simply clear the "back-stack" rather than override the behavior for the back-button:
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
This is a safer solution that can also be used after you log-in and not just after you log-out.
public void onBackPressed(){
if(appCanClose){
finish();
}
}
These functions can exist in both the system framework (used if not in your code), as well as in your code. If you leave it empty, the app will do nothing when the back button gets pressed.
In this example, when the boolean value appCanClse is true, the back button will quit the app, if false, the back button wil do nothing. I would make sure the user still has someway to quit the app. :p
You can do it by just adding this two line of codes
#Override
public void onBackPressed(){
moveTaskToBack(true);
}
It will prevent going back to previous activity as well as take the app to background when anyone hits back button
The actual solution is
#Override
public void onBackPressed() {
super.onBackPressed();
finishAffinity();
}
add this code in Login Activity. App closes when back button clicked in login page.

Iconsistent results when returning to a previous activity

I have an android app that has various activities.
for example there is a home screen and an update screen.
The home screen is a list activity.
A button on the home screen brings you to the update screen that updates the database from a server. In theory when Returning to teh homescreen after an update, the list should be changed to reflect the update just done.
the code for going to the update screen is as follows:
Button synch = (Button) findViewById(R.id.synchButton);
synch.setOnClickListener(new OnClickListener() {
public void onClick(View viewParam) {
Intent intent = new Intent(HomeScreen.this, SynchScreen.class);
startActivity(intent);
}
});
and the code for returning back to the homescreen after the updates is:
main_menu.setOnClickListener(new OnClickListener() {
public void onClick(View viewParam) {
finish();
}
});
the list is compiled from an async task that runs in onStart, so my understanding is that onStart should run when I return to the homescreen, thus always displaying the most up to date version of the list.
On my Emulator I always get teh updated list, but when I run it on my phone the list is never updated, it just returns to the state of teh screen before I did the update.
any ideas?
thanks
Kevin
Check the Activity lifecycle section of the Android documentation. The code updating the view should probably be moved to onResume, since the Activity might not get killed when launching a new one.
Put the code for starting the Asynctask in onResume. Read the documentation related to activity life cycle.
#Override
public void onResume() {
super.onResume();
Apaptor.refreshListView(Vector);
}

Categories