简体   繁体   中英

jumping to another activity using Thread.sleep()

i'm trying to jump from activity one to activity two but after 1 second. So i used Thread.sleep(1000) and after that the activity two comes to front.Its working good but problem is I have given a image background in activity one which is not shown.

 public class Activity1 extends Activity {
 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        try {

        Thread.sleep(1000);
        Intent i=new Intent(Activity1.this,Activity2.class );
        startActivity(i);
        } catch (InterruptedException e) {

            e.printStackTrace();
        }

    }

additionally I included a button in Activity two which does the functionality of jumping to Activity one, in this case the image is shown for 1 second but not the first time when I open up my app.

You UI is not built until after onCreate() finishes. Therefore, if you start activity 2 in onCreate(), you will never see activity 1.

Instead:

@Override
void onAttachedToWindow()  {
    // start activity 2
}

As others have said in their comments, you should never use sleep on the UI thread. Indeed, I wish that Java made it much harder to use sleep because it is used far too much and often as a quick fix which avoids the right solution.

In this case, you should use an AsyncTask to start the second activity after one second.

You can create RelativeLayout and show/hide it and use 1 Activity only

public class MyView extends RelativeLayout{

 private ImageView view = ....;

 ....
 view.setVisibility(View.VISIBLE);
 ....
 view.setVisibility(View.GONE);

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM