0

I want that the fragment enter after the creation of activity instead enter togheter

My code

import android.os.Bundle;
import android.app.Fragment;
import android.app.FragmentTransaction;

public class TranslucentActivity extends Activity {
 //....


@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.translucent);
    }

    @Override
protected void onResume() {
    // TODO Auto-generated method stub

    android.app.Fragment fragment = new ViewDetail();
    android.app.FragmentManager fragmentManager = getFragmentManager(); 
    android.app.FragmentTransaction ft = fragmentManager.beginTransaction(); 
    //ft.setCustomAnimations(R.anim.trans_left_in,R.anim.trans_left_out);
     ft.setCustomAnimations(R.animator.trans_left_in,R.animator.trans_left_out);// now work but is wrong the animation because enter with the activity
    ft.addToBackStack(null); 
    ft.replace(R.id.container, fragment); 
    ft.commit();
    fragmentManager.executePendingTransactions();
    super.onResume();
}

 }

trans_left_in.xml

  <set xmlns:android="http://schemas.android.com/apk/res/android"> 
     <translate 
        android:fromXDelta="100%p" 
        android:toXDelta="0" 
        android:duration="@android:integer/config_longAnimTime"/>
  </set>

trans_left_out.xml

  <set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
       android:fromXDelta="0" 
       android:toXDelta="100%p" 
       android:duration="@android:integer/config_longAnimTime"/>
  </set>
Ortensia C.
  • 4,666
  • 11
  • 43
  • 70
  • What is your API? Check that out http://stackoverflow.com/questions/17760299/android-fragmenttransaction-custom-animation-unknown-animator-name-translate – Gokhan Arik Jan 23 '14 at 16:31
  • My API is Android 4.4.2 – Ortensia C. Jan 24 '14 at 08:10
  • Instead of anim, you need to create `animator` folder and use `R.animator.yourid` – Gokhan Arik Jan 24 '14 at 08:13
  • I created the folder and now is no longer the error, but the animation is wrong, the fragment and the activity coming from the right together it should be animated only the fragment – Ortensia C. Jan 24 '14 at 08:45
  • What do you mean coming together? Your activity is already active, you are just creating a fragment, how come activity is coming? I didn't understand what you mean. – Gokhan Arik Jan 24 '14 at 08:57
  • It is about the lifecycle of the fragment. `onResume` is called right after `onCreate`. You are creating activity and as soon as you create it you are creating a fragment as well. So try to put a timer there, create fragment 1-2 seconds later maybe. Or search for transition features, it might have a feature something like `delay` – Gokhan Arik Jan 24 '14 at 09:22

1 Answers1

0

I found this solution. I put a timer and I changed method

private Fragment fragment;
private FrameLayout frame;
private android.app.FragmentTransaction ft;
private android.app.FragmentManager fragmentManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.translucent);

        frame =(FrameLayout)findViewById(R.id.container);
  }

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    // TODO Auto-generated method stub
    super.onWindowFocusChanged(hasFocus);
            frame.postDelayed(new Runnable() {

        @Override
            public void run() {
                // TODO Auto-generated method stub
           fragment = new ViewDettaglioCliente();
           fragmentManager = getFragmentManager(); 
           ft = fragmentManager.beginTransaction(); 
           ft.setCustomAnimations(R.animator.trans_left_in,R.animator.trans_left_out);
           ft.addToBackStack(null); 
           ft.replace(R.id.container, fragment); 
           ft.commit();


            }
        }, 500);
}
Ortensia C.
  • 4,666
  • 11
  • 43
  • 70