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.
Related
I'm developing an app. for my semester project.App. is simple true&false game.I did something about it but i choked up one point.My problem is that handling activities.
Here is the quick view of the design.
http://imgur.com/2Fhsrn8
I created all activities and my codes working correct.But there is an issue.When i'm on third or fourth page(activity) and press back button on my phone it returns the second page then i automatically have another chance to answer question.All i want is turn the first screen when back button is pressed and clear all the data such as assign score as 0.
I'will be grateful for any kind of help.
Use
finish();
for your current activity when you start the new intent(e.g. when going from activity 3 to activity on your picture) and add this to your activity's 2-3 if you want to start again when you click back, otherwise it will close your app.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ((keyCode == KeyEvent.KEYCODE_BACK))
{
//implement code to start 1st activity here!
}
return super.onKeyDown(keyCode, event);
}
you should override onBackPressed on your third, and fourth activity ->
#Override
public void onBackPressed() {
// here you should create intent to your firstActivity
// and assign variables -> e.g.
// score = 0;
}
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.
Ia m using ViewFlipper in my app, and to move inside the pages, I use a Button for next and capturing the onBackPressing to return back.
the behavior is the following:
1) I click on button and move to 2 page.
2) click back and code work
3) click again on the button next
4) click back and now wont work anymore
on the step 4, I can feel the vibration, so the event fire, but my viewflipper wont to go back.
Any suggestion?
Thank's
public void onBackPressed() {
if (flipView.getDisplayedChild() == 1) {
flipView.setDisplayedChild(0);
} else if (flipView.getDisplayedChild() == 0) {
flipView.setDisplayedChild(1);
}
}
This works perfectly for me. Change onBackPressed to what ever method is calling the back button.
Just to add something to the other answer.
Let's say we have only two views that we are flipping through, doing:
public void onBackPressed() {
if (mViewFlipper.getDisplayedChild() == 1) {
mViewFlipper.setDisplayedChild(0);
} else if (mViewFlipper.getDisplayedChild() == 0) {
flipView.setDisplayedChild(1);
}
}
isn't enough. As a matter of fact, it creates another problem for you.
If the view is in 0 (the first), and you then press the back button, NOTHING happens. The activity doesn't exit. This is because you haven't called super.onBackPressed(). Now, adding super.onBackPressed() to the code above also creates another problem. When you flip from 1 (the second view) it goes to the first view (0) and then exits the activity, which is wrong if not for anything but for the weird animation of skipping a view while transitioning from one activity to another.
The best way to implement onBackPressed() for your activity containing a ViewFlipper is this:
public void onBackPressed() {
int displayedChildId = mViewFlipper.getDisplayedChild(); //get current view's number
if (displayedChildId > 0) { //if this number is greater than 0(let's say 5)
mViewFlipper.setDisplayedChild(displayedChildId - 1);//We then go down that number by 1. That is 5 - 1, which is 4. This happens until displayedChildId isn't greater than 0 anymore, which is then the first view. if we press back from here, we exit the activity.
} else {
super.onBackPressed();
}
}
I hope this makes sense
When I press the search key on my device, I want it to show a white background. However, when I press the back button, I want the previous activity's background to be restored. ivBackground is a variable I added to my relativelayout which I turn VISIBLE to show the white background.
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_SEARCH) {
ivBackground.setVisibility(View.VISIBLE);//WHITE IMAGEVIEW
return false;
} else if (keyCode == KeyEvent.KEYCODE_BACK) {
ivBackground.setVisibility(View.GONE);
return true;
}
return super.onKeyDown(keyCode, event);
}
While the above code works, the problem is that when I press the back button, the white screen still remains. It only goes away if I press the back button once again. Any solutions?
On the relevant activity, you can get a reference to a SearchManager object. On this, you can set an OnDismissListener, which is called the when search UI is dismissed e.g.
this.searchMgr = (SearchManager)this.getSystemService(Context.SEARCH_SERVICE);
this.searchMgr.setOnDismissListener(new OnDismissListener() {
public void onDismiss() {
ivBackground.setVisibility(View.GONE);
}
});
To make the white background visible, you can override onSearchRequested inside your activity class, which is called when a user signals the desire to start a search
#Override
public boolean onSearchRequested() {
ivBackground.setVisibility(View.VISIBLE);
return super.onSearchRequested();
}
Hope this helps!
How about setting a SearchView.OnCloseListener or a SearchManager.OnDismissListener that also hides your view? This seems like the right solution anyway since the two events are logically connected. If you later dismiss the search view programmatically for example, you don't have to worry about separately dismissing the background view.
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.