- Replace your activity to FragmentActivity
- implement GridView and set the adapter in the fragment constructor/onCreateView functions
- Replace Fragment with FragmentTransaction setCustomAnimations function.
very simple to implement
Question
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?
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
very simple to implement
OTHER TIPS
It is not possible using activities because you can't assign content to a View
until it'll be inflated, which happens by setContentView()
in Activity, usually in OnCreate()
: so Activity
has to be started first. But, since only one Activity
instance could be active at one time, you can't do it while you are inside your first Activity
.
Workaroud: use Fragment
s. You can have many instances of Fragments at one time, therefore, you can show to the user, for example, Fragment1
while loading Fragment2
. ViewPager is often used in conjuction with fragments and it loads three pages by default, which means that while you see a page in ViewPager
, previous and next pages and corresponding fragments are actually being loading or already loaded. You can use similar technique for your task.