I'm in trouble handling onAnimationEnd, let me explain: TopBar is the main activity which contain all fragments so I'm trying to put some code in other classes and call in TopBar when I need it, and it works, when I click for example on the profile button the SlideLeftIn(view,context) work great, but if I click the back button then the animation not start and the mainpage (page0) start immediatly, so, trying to wait the end of animation, I made another method in MyAnim in wich I insert the setPage() in the OnAnimationEnd, so I have SlideLeftOut(view,context,page) and then I get the illegalstateexception, and I searched for the solution but can't find out the answer for my case, this is my main activity TopBar:
public class TopBar extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Remove title bar
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.top_bar);
if( savedInstanceState == null ) {
setPage();
}
//many button.onclicklistener for choose the page for fragment
}
@Override
public void onBackPressed() {
if (main==0) {
if (profl==1) {
profl=0;
myanim.SlideLeftOut(profview,TopBar.this,0);
} else {
setPage();
}
} else {
finish();
}
}
public void setPage() {
setPage(0);
}
public void setPage(int pg) {
Frammento fg = new Frammento();
Bundle args = new Bundle();
args.putInt("PAGINA_FRAGMENT", pg);
fg.setArguments(args);
FragmentManager fragmentManager = getSupportFragmentManager();
if (pg==1) {
fragmentManager.beginTransaction().replace(R.id.searchbar, fg).commit();
} else {
fragmentManager.beginTransaction().replace(R.id.pageFragment, fg).commit();
}
}
public class Frammento extends Fragment {
public Frammento() {}
@Override
public View onCreateView(LayoutInflater inf,ViewGroup contenitore,Bundle savedInstanceState ){
View radice;
int pag = getArguments().getInt("PAGINA_FRAGMENT");
switch(pag) {
case 0:
radice = inf.inflate(R.layout.logged,contenitore,false);
break;
case 1:
radice = inf.inflate(R.layout.srcbarlayout,contenitore,false);
break;
case 2:
radice = inf.inflate(R.layout.insertnew,contenitore,false);
break;
case 3:
radice = inf.inflate(R.layout.searchevent,contenitore,false);
break;
case 4:
radice = inf.inflate(R.layout.myprofile,contenitore,false);
break;
case 5:
radice = inf.inflate(R.layout.message,contenitore,false);
break;
case 6:
radice = inf.inflate(R.layout.invitation,contenitore,false);
break;
case 7:
radice = inf.inflate(R.layout.mostraevento,contenitore,false);
break;
default:
radice = inf.inflate(R.layout.logged,contenitore,false);
break;
}
pageManager(pag,radice);
return radice;
}
}
pageManager(pag,radice) simply do operations for the choosen layout, and this is MyAnim.java:
public class MyAnim {
public void SlideLeftIn(final View view, Context c){
Animation animation = AnimationUtils.loadAnimation(c, R.anim.left_slide_in);
animation.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {}
@Override
public void onAnimationRepeat(Animation animation) {}
@Override
public void onAnimationEnd(Animation animation) {}
});
view.startAnimation(animation);
}
public void SlideLeftOut(final View view,final Context c,final int i){
Animation animation = AnimationUtils.loadAnimation(c, R.anim.left_slide_out);
animation.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {}
@Override
public void onAnimationRepeat(Animation animation) {}
@Override
public void onAnimationEnd(Animation animation) {
view.setVisibility(View.GONE);
TopBar main = new TopBar();
main.setPage(i);
}
});
}
}
the xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:background="@color/background"
android:id="@+id/Layout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<ImageButton
android:id="@+id/srcbt"
style="?android:attr/buttonStyleSmall"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:layout_toLeftOf="@+id/newevent"
android:layout_alignParentTop="true"
android:background="@drawable/search" />
<ImageButton
android:id="@+id/newevent"
style="?android:attr/buttonStyleSmall"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:layout_toLeftOf="@+id/ticket"
android:layout_alignParentTop="true"
android:background="@drawable/add" />
<FrameLayout
android:id="@+id/searchbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/topbar" >
</FrameLayout>
<FrameLayout
android:id="@+id/pageFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/searchbar" >
</FrameLayout>
</RelativeLayout>
and finally this is logcat:
06-15 14:40:27.926: D/AndroidRuntime(20790): Shutting down VM
06-15 14:40:27.931: W/dalvikvm(20790): threadid=1: thread exiting with uncaught
exception (group=0x41c0d700)
06-15 14:40:27.931: E/AndroidRuntime(20790): FATAL EXCEPTION: main
06-15 14:40:27.931: E/AndroidRuntime(20790): java.lang.IllegalStateException: Activity
has been destroyed
06-15 14:40:27.931: E/AndroidRuntime(20790): at
android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1365)
06-15 14:40:27.931: E/AndroidRuntime(20790): at
android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:595)
06-15 14:40:27.931: E/AndroidRuntime(20790): at
android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:574)
06-15 14:40:27.931: E/AndroidRuntime(20790): at
com.android.seeya.TopBar.setPage(TopBar.java:220)
06-15 14:40:27.931: E/AndroidRuntime(20790): at
com.services.MyAnim$9.onAnimationEnd(MyAnim.java:173)
06-15 14:40:27.931: E/AndroidRuntime(20790): at
android.view.animation.Animation$3.run(Animation.java:379)
06-15 14:40:27.931: E/AndroidRuntime(20790): at
android.os.Handler.handleCallback(Handler.java:730)
06-15 14:40:27.931: E/AndroidRuntime(20790): at
android.os.Handler.dispatchMessage(Handler.java:92)
06-15 14:40:27.931: E/AndroidRuntime(20790): at
android.os.Looper.loop(Looper.java:137)
06-15 14:40:27.931: E/AndroidRuntime(20790): at
android.app.ActivityThread.main(ActivityThread.java:5493)
06-15 14:40:27.931: E/AndroidRuntime(20790): at
java.lang.reflect.Method.invokeNative(Native Method)
06-15 14:40:27.931: E/AndroidRuntime(20790): at
java.lang.reflect.Method.invoke(Method.java:525)
06-15 14:40:27.931: E/AndroidRuntime(20790): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
06-15 14:40:27.931: E/AndroidRuntime(20790): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
06-15 14:40:27.931: E/AndroidRuntime(20790): at
dalvik.system.NativeStart.main(Native Method)
06-15 14:45:28.076: I/Process(20790): Sending signal. PID: 20790 SIG: 9
Thanks in advance for anyone who just think to try to help me, any help will be really appreciated.