tags:

views:

21

answers:

1

Hi there,

when I add a ViewFlipper, the UI thread seems to wait for the onCreate() method in the activity to be finished. Then it shows the second view. Why does it happen?

My current code is:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_menu);    

    viewFlipper = (ViewFlipper) findViewById(R.id.ScreenSwitch);
    viewFlipper.setInAnimation(AnimationUtils.makeInAnimation(this, true));
    viewFlipper.setOutAnimation(AnimationUtils.makeOutAnimation(this, true));

    //do the necessary loading, when the splash screen persists 
    doSomeLoading();

    viewFlipper.showNext();

}

Actually, the doSomeLoading consists of a for loop counting to ten millions and doing nothing. Now it just waits for loop to be done and shows the second view.

I would really appreciate a solution without having to create a separate Thread, because it seems to be pointless, invalidate() doesn't help there.

A: 

Maybe with viewFlipper.showPrevious();?

EDIT

I don't have your full code but here is the idea :
When you do long loading or slow actions, most of the time, you can think AsynTask or background thread.
So you need to create a new inner class. Lets say AsyncLoader. You will implement the method doInBackground() of this class and put your doSomeLoading() in it.
Now, implement onPostExecute() and put your viewFlipper.showNext(); in it.
Then, in your onCreate() method, replace the doSomeLoading() by new AsyncLoader.execute();
This should be nice. I might have forgotten some stuff as it's not real code. Check this for more explanations.

Sephy
Nope, it still refreshes the view after the doSomeLoading() - the screen is black, when inside the doSomeLoading(). And then the desired view pops up.
LordTwaroog
Are you executing your loop in the UI thread or in a background thread? because if it is in the UI thread, it can explain why you have the black screen. The system finishes the doSomeLoading code before executing the next line.
Sephy
But at first I want to have the first view. After loading, the second (with viewFlipper.showNext() ) is shown.The conception changed a bit, so I just perform loading in separate, start activity. The thread is started and after loading it starts a new activity and then finishes itself. But this looks a bit ugly. I was trying to find a bit more elegant way to do this.
LordTwaroog
ok, check my edit, this could be a more elegant way maybe.
Sephy
Is there any significant difference between AsyncTask and old-fashioned thread in means of performance and system resources usage?
LordTwaroog
No idea..sorry.You should ask that in google developer google group...
Sephy
Anyway, thanks for help and your time ;)
LordTwaroog