Reputation: 4997
Whenever I press the up button from my current activity, parent fragment slides in towards the left from the right edge, which seems to be the default transition behavior.But i want the parent activity to slide in rightwards from the left edge.
I already used onBackPressed
with overridePendingTransition
and custom transition xml files but my onBackPressed
is never called.
Here is my java code.
public class TakeOrderItems extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_take_order_items);
//Get the actionbar to set the back button
ActionBar ab = getSupportActionBar();
ab.setDisplayHomeAsUpEnabled(true);
/*
CODE TO CREATE AND DISPLAY EXPANDABLELISTVIEW
*/
@Override
public void onBackPressed() {
super.onBackPressed();
Log.i("Good","back pressed");
overridePendingTransition(R.transition.slide_in,R.transition.slide_out);
}
XML transition files
slide_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="-100%"
android:toXDelta="0"
android:duration="200" />
</set>
slide_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="0"
android:toXDelta="100%"
android:duration="200" />
</set>
I have checked many answers but they seems to be confusing and contradictory.I think there must be an elegant and efficient way to achieve this since many many apps use this.Please please help me.Thanks.
Upvotes: 2
Views: 468
Reputation: 6968
The Up navigation button is handled by onOptionsItemSelected
. So override it:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
Note that this onOptionsItemSelected
implementation assumes that your parent activity is already started, and is the next one on your backstack. You'll may want to start it in addition to finish the current one.
Override the finish
method of your Activity
to set the transition:
@Override
public void finish() {
super.finish();
overridePendingTransition(R.anim.slide_in, R.anim.slide_out);
}
Also, it looks like you're confused by the overridePendingTransition
method name. It takes animations as parameters, not transitions.
This would be your slide_in
equivalent:
<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
android:fromXDelta="-100%"
android:toXDelta="0%"
android:duration="200"/>
</set>
And the slide_out
:
<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
android:fromXDelta="100%"
android:toXDelta="200%"
android:duration="200"/>
</set>
Just put those files in res/anim
instead of res/transition
.
Upvotes: 0
Reputation: 695
Put overridePendingTransition
above call super.onBackPressed()
it must work
Upvotes: 1