Search code examples
androidandroid-gridviewpending-transition

Android: How to fill GridView programatically before starting transition?


I have an MainActivity A, from which the user can select different items which are displayed in Activity B in a GridView. Between those Activities I have a Sliding-Transistion (Activity A slides out to the left, Activity B slides in from right)

My problem is, that if the animation starts, Activity B screen is not created complete (GridView items are added programmatically), so I see an empty screen sliding in from the right, which looks dump.

So my question: Is it possible to create/load Activity B before transition starts?

enter image description here

I attach some code, but all is pretty straight forward. Only the grid items consists of images with different states...

Activity A: start Intent (as usual)

 Intent intent = new Intent(this, SoundShareActivity.class);
 //...
 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
 startActivity(intent);
 overridePendingTransition(anim.slide_in_from_right_to_left,
            anim.slide_out_to_left); 
 //anim defined in xml

Activity B: onCreate

 protected void onCreate(Bundle savedInstanceState) {
 //...
 mGridView = (GridView) findViewById(R.id.grid_view_sounds);
 mAdapter = new SoundAdapter(getApplicationContext(), THE_DATA);
 mGridView.setAdapter(mAdapter);
 mGridView.setOnItemClickListener( ... );
 mGridView.setLongClickable(true);
 mGridView.setOnItemLongClickListener(...);

Solution

    1. Replace your activity to FragmentActivity
    2. implement GridView and set the adapter in the fragment constructor/onCreateView functions
    3. Replace Fragment with FragmentTransaction setCustomAnimations function.

    very simple to implement