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
Related
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.
While using a sencha touch app on Android, is it possible that the device back button behaves completely as it behaves for a native Andriod app? This thread addresses the same problem. The suggested solution is
if (Ext.os.is('Android')) {
document.addEventListener("backbutton", Ext.bind(onBackKeyDown, this), false);
function onBackKeyDown(eve) {
eve.preventDefault();
//do something
alert('back button pressed');
}
}
but in this function, how can i go to the previously visited screen?
Of course it depends on how your user interface is built and how the views are connected. Simplest way I can think of is to detect somehow the presence of a back button in the page and trigger a click on it.
In my application each view that needs to handle the backbutton event has a button with cls = 'back' that implements a tap listener responsible of the logic to go back to the previous view.
About how to implement this logic: there is no magic wand for that. If a particular view can be reached through one and only one parent view, then you can hardcode it. Else, if a view can be reached from more than one, I save in a property of the view's controller a reference to the view which I came from, and then on backbutton's tap I move to that view.
The animation of moving to a view can usually be obtained by Ext.Viewport.animateActiveItem(parentView, {type: 'fade'}); (but again it depends on how you implemented your views tree)
So in my handler for the backbutton I check for such a button with [cls=back] (but you could also check for ui or whatever you want) and I fire the tap event on it.
Moreover, when i detect I am on the root view I ask the user if he wants to exit the application and if so I call exit().
_handleBackButtonEvent : function(e) {
// First look if the current view is the mainView
var item = Ext.Viewport.getActiveItem();
// If we are on mainView, quit.
// Sayonara, goodbye, adios
if (item.getId() === 'mainView') {
navigator.notification.confirm(
"Really quit?",
function(option) {
if (option == 1) {
navigator.app.exitApp();
}
},
"Confirm",
"Yes,No"
);
} else {
// else look if the current view has a backbutton
var titlebar = item.down('titlebar[docked=top]');
var backbutton = null;
if (titlebar) {
backbutton = titlebar.down('button[iconCls=back]');
}
if (backbutton && !backbutton.isHidden() && !backbutton.isDisabled()) {
backbutton.fireEvent('tap');
} else {
// if not, fallback to mainView
item = Ext.getCmp('mainView');
Ext.Viewport.animateActiveItem( item, {type: 'slide', direction: 'right'});
}
}
e.preventDefault();
}
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.
I am developing an application using TabHost. I am using android default back button to move back to previous activity from current activity by overriding onBackPressed() method inside ActivityGroup of each tab.
Now , the problem is , in one of my activity i have an EditText which get focused when the activity starts. Then , if i press back , it does not go to previous activity , instead it closes the application. By searching about the problem on internet what i have found is that when the EditText get focused which is a child view of activity's view , the activity loss focus and then if back button is pressed , for lack of focus on the current activity it closes the application. Still i am little bit confused or can be say not clear about the problem.
So, any how , i have managed to set and remove focus over EditText on run time using code. But still now , as the EditText does not have focus , if the back button is pressed , it closes the application. I am really confused what's actually going on. So , if anyone have any idea or solution about the problem , please help on the issue. I will appreciate that very much. Thanks.
You can override this behavior by adding Key Listener to your EditText. Try this out,
name_edit.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN
&& event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
Log.i("Back event Trigered", "Back event");
activitygroup.back();
}
return false;
}
});
try this..
#Override
public void onBackPressed() {
onKeyDown(KeyEvent.KEYCODE_BACK, new KeyEvent(KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_BACK));
super.onBackPressed();
}
try this
public void onBackPressed() {
startActivity(new Intent(currentActivity.this, previousActivity.class));
finish();
}