Animation overriding onNavigateUp's default one - android

I am launching an About Activity with overridePendingTransition() right after it, so I can get an animation of the incoming Activity.
I want the Activity to perform an animation as well upon "leaving", so I have overriden onBackPressed() and it works ok.
The problem comes, as the About activity has the "Up navigation" enabled, on how to perform the animation when the "Up" navigation is tapped --instead of just Back button-- to return to previous activity.
I have tried
#Override
public boolean onNavigateUp() {
overridePendingTransition(R.anim.fadeinltr, R.anim.fadeoutltr);
return super.onNavigateUp();
}
but it does not work, because by the time the overridePendingTransition() method is called, there is no transition to override yet.
Any ideas?

I had the same problem and I solved in this way:
#Override
public boolean onNavigateUp(){
boolean x = super.onNavigateUp();
overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_right);
return x;
}

Related

Ripple effect activated on backpressed

as title says, how to make my ripple effect on ImageButton (ripple effect assigned to this ImageButton) activated on backpressed() without back to the previous activity
with a bit of logic you could achieve that easily. what i thought of is that you have a method lets call it doSomething();
boolean onBackpressed = false;
private void doSomthing() {
if(onBackpressed){
finish();
}else{
// do anything else that the button wants to do;
}
}
now onBackpressed() you could do this
#Override public void onBackpressed() {
onBackpressed = true;
myImageButton.performClick(); // myImageButton.callOnClick()
}
P.S: that i didn't call super.onBackpressed(); so we can control the back press without existing the app.
of course your imageButton click listener calls doSomething() method.

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.

Android: Finish activity when SlidingMenu (jfeinstein) is showing

I'm trying to implement the sliding menu created by jfeinstein in my app. The behavior I want to achieve is that when I press the back button and the menu is not showing, it will show up. This works great.
However when the menu is showing pressing the back button should finish the current activity. What happens instead is that the sliding menu is just closed again.
To achieve the described behavior I have overwritten onBackPressed:
#Override
public void onBackPressed()
{
SlidingMenu sm = getSlidingMenu();
if(!sm.isMenuShowing())
{
sm.showMenu();
}
else
{
finish();
}
}
But as soon as the sliding menu is showing onBackPressed isn't called anymore. I suspect as I have to use a SlidingFragmentActivity pressing the back button triggers the fragment history stack to pops.
Does anybody know how to solve this problem?
After browsing the SlidingMenu code a little further I found that onKeyUp is overwritten in SlidingFragmentActivity. So the way to go is to override onKeyUp in your activity.
#Override
public boolean onKeyUp(int keyCode, KeyEvent event)
{
if(keyCode == KeyEvent.KEYCODE_BACK)
{
SlidingMenu sm = getSlidingMenu();
if(sm.isMenuShowing())
{
finish();
return true;
}
}
return super.onKeyUp(keyCode, event);
}

Listen for first TouchEvent when using SYSTEM_UI_FLAG_HIDE_NAVIGATION

Starting with API 14 you can use this flag SYSTEM_UI_FLAG_HIDE_NAVIGATION on a View within your Activity to tell the system you want to hide the navigation bar until there is user interaction (screen touch). Once the user taps the screen the bar is shown.
The Activity that I am doing this in takes some action (starts a separate activity) when the user touches the screen, before adding the above flag to my view this worked perfectly.
After adding that flag the system intercepts the first screen touch and reacts to it by showing the nav bar. It's not until the second touch that any of my Views, or my Activity are receiving a TouchEvents.
Does anyone know of a way that I can set up a listener that will let me launch my second activity the first time the screen is touched instead of needing to double tap when using this hide nav flag?
I've tried all of the following and I'm not getting callbacks to any of them when the screen is touched for the first time to show the nav bar.
#Override
public void onUserInteraction(){
Log.i(myTag, "INTERACT");
}
#Override
public boolean onGenericMotionEvent(MotionEvent me){
Log.i(myTag, "GENERIC");
return true;
}
//I thought maybe the size change would lead to a callback here. No dice though.
#Override
public void onWindowAttributesChanged(WindowManager.LayoutParams params){
Log.i(myTag, "WINDOW CHANGE");
}
#Override
public boolean dispatchTouchEvent(MotionEvent me){
Log.i(myTag, "TOUCH");
return true;
}
Note: I am not trying to prevent the nav bar from being shown upon the first touch, I just want to also take some other action when that event occurs.
As Josh Lee suggested in his comment, View.OnSystemUiVisibilityChangeListener was the key.
Here is the code that I used:
mView.setOnSystemUiVisibilityChangeListener(new OnSystemUiVisibilityChangeListener() {
#Override
public void onSystemUiVisibilityChange(int vis) {
Log.i(myTag, "System UI"+ vis);
if(vis == 0){
Intent i = new Intent(MainActivity.this, AnotherActivity.class);
startActivity(i);
finish();
}
}
});
I think that mView could be a reference to any view that is currently showing in your Activity. In my case it was a fullscreen VideoView, and was the only view in my layout.

Manage the back button with multiple scene

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.

Categories

Resources