Manage the back button with multiple scene - android

I followed this tutorial http://www.andengine.org/forums/tutorials/multiple-screen-andengine-game-v2-t4755.html to create a simple application with multiple scene and only one activity.
I'd like to know how can i can return to the previous scene when i use the back button and finish the activity when i'm in the first scene.
I tried so in the MultiScreen Class:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
this.mEngine.getScene().back();
}
return super.onKeyDown(keyCode, event);
}
replacing the core.getEngine().setScene(scene); in the SceneManager with this.mEngine.getScene().setChildScene(scene);
scene work differently from how I understood, I resolve with:
#Override
public void onBackPressed()
{
Scene scene = this.mEngine.getScene();
if(scene.hasChildScene()){
scene.back();
}
else{
this.finish();
}
}

You can override the back key in one of two ways, either by overriding the onBackPressed() method, or the dispatchKeyEvent() method
Overriding onBackPressed:
#Override
public void onBackPressed()
{
// your code here
}
Overriding dispatchKeyEvent:
#Override
public boolean dispatchKeyEvent(KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK)
{
// your code here
}
return (yourbooleanhere);
}

I had a dilemma like you in that sense. I explain. I have an activity like works with a ViewFlipper. I need to go to the different "layout" that I wrote on ViewFlipper tag and, when a I was in a particular "layout" that is not the main "layout" of the activity, I need to come back to the main pressing the back button, but, it doesn't work what I want.
To resolve this dilema, I override OnBackPressed function like:
#Override
public void onBackPressed() {
if (condition) {
// go to the main
} else {
super.onBackPressed();
}
}
where condition, as name says, how do you know that you are in the main "layout"? For example, in my ViewFlipper, the main "layout" are ordered by numbers, that begin on 0, so my main "layout" is 0 and how I know that I am in the 0 "layout", viewflipper.getDisplayedChild(). If it returns 0, I stay in the main "layout" or not, otherwise.
So simple but, I know, not so elegant. It's an idea, I think that can help you.

Related

How to make device back button cause onCreate?

When you press back in ActionBar, it causes onCreate on the previous activity. How to do that with device back button aswell? In preference activity, if a certain preference is changed, I want main activity to be re-created when user presses back on bottom of their device.
You can override onBackPressed() and start your Activity yourself.
You call onNavigateUp() at any time in your Activity to get the same behavior as clicking the up button.
For example:
#Override
public void onBackPressed() {
if (myPreferenceHasChanged) {
onNavigateUp();
} else {
// Use the default behavior
super.onBackPressed();
}
}
Did you try something like this?This should solve your question
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
// your code
return true;
}
return super.onKeyDown(keyCode, event);
}

Handling back button in case of multiple setContentView's

I have an activity, say FirstActivity, and multiple views that are utilized by it.
Views are switched by clicking buttons on screen.
Currently, I handle back button (to return to previous view) this way:
#Override
public boolean onKeyDown (int keyCode,KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
if (current == ViewNum.VIEW_DEFAULT) {
return super.onKeyDown(keyCode,event);
}
else {
setContentView(R.layout.firstactivitynew);
configureUI();
return true;
}
}
else {
return super.onKeyDown(keyCode,event);
}
}
}
This, obviously, works only if I have one nested contentView, and this is a case.
However, I have a feel that there is something terribly wrong with this way of handling this. Is there any 'standard' way to handle this?
I think you should make use of Tabs and Fragments, and should forget about the possibility of changing the activity view with setContentView multiple times.
http://developer.android.com/training/implementing-navigation/lateral.html
But technically, you can also keep track of your "application state" and redefine the onBackPressed() method to take your view back to the previous view at a given state.
I think you should make use of the onBackPressed() method:
Called when the activity has detected the user's press of the back
key. The default implementation simply finishes the current activity,
but you can override this to do whatever you want.

Is it possible for the Back button to take the user to a specific Activity given certain conditions, and behave like normal in other conditions?

I'd like the back button to have behaviour similar to e.g.
// if I have an Item count > 1 and I'm not on ItemsListActivity
// back button takes me to ItemsListActivity
// else
// back button behaves like normal
Is this possible? If so, what do I need to do?
Yes.
Override onBackPressed()
(http://developer.android.com/reference/android/app/Activity.html#onBackPressed())
Called when the activity has detected the user's press of the back key. The default implementation simply finishes the current activity, but you can override this to do whatever you want.
So you could do something like:
#Override
public void onBackPressed(){
if (count > 1) && (!(this instanceof ItemsListActivity)) {
// Launch ItemsListActivity / do whatever you want
}
else {
super.onBackPressed(); // Do the normal back press functionality
}
}
There is probably a better way to check what activity you are in!
Is it possible: you have to override onKeyDown:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// Check if the key event was the back button
if ((keyCode == KeyEvent.KEYCODE_BACK) && itemCount > 1) {
// Back button takes you to ItemsListActivity
return true;
}
// Otherwise execute requested action
return super.onKeyDown(keyCode, event);
}
By the way, when user hit back, normally they expect to return on previous activity or exit app. IMHO it is better to make another way (button or menu item) to access on your ItemsListActivity.

what are the methods invoked in the underlying activity when the back button is pressed?

now in my application I invoked another activity on the onItemLongClickListener() of my listView. I edited the data in my next activity , there on save button click I updated the database and finished the activity . how to get the list to be refreshed automatically? I used notifyDataSetChanged on my adapter in the OnStart and OnResume event . It doesn't seem to work . Can anyone please suggest me what can I do ?
Android 2.0 API introduced the onBackPressed() method, its recommended to override that method instead:
#Override
public void onBackPressed() {
return;
}
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
//Back button pressed
}
return true;
}

method to call when home button clicked?

In My android application, currently one of my activity is focused on the screen. If the user clicked on "HOME" button, which activity method gets invoked.
You cannot override anything about the Home activity. You can only use the Home intent category.
See this other question for more info:
Can I override the 'Home' button in my application?
There is no way to handle "Home" button click. And this is by design.
Read great post by CommonsWare: Please Ignore the HOME Button. Also see this and this threads on Android Developers group.
Works for me..
#Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
}
Then
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_HOME){
// Your code here.
return true;
}
return false;
}

Categories

Resources