Saint Fayson
Saint Fayson

Reputation: 91

How to display a screen only for some seconds Android Studio

I am new in Android Development. I have been trying to figure out how to display a screen in android studio only for 5 seconds and then get transfered into a new activity.

For example: Activity A -> Activity B (Shown for 5 seconds) -> Activity C

Also I want to make sure that when a user clicks on the back button while he is in Activity B nothing happens (It doesnt go back to Activity A).

What is the easiest way to do that? I know I have to use Intent.

Upvotes: 1

Views: 951

Answers (2)

Rafael K
Rafael K

Reputation: 171

try this. I have commented it out, but if you have any questions about it feel free to ask.

public class ClassB extends AppCompatActivity {


//Handler allows you to send and process Runnable Objects (Classes in this case)
private Handler mHandler = new Handler();


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_classb);


    //postDelayed method, Causes the Runnable r (in this case Class B) to be added to the message queue, to be run
    // after the specified amount of time elapses.
    mHandler.postDelayed(new Runnable() {
        @Override
        public void run() {
            //Create a new Intent to go from Class B to Class C and start the new Activity.
            Intent intent = new Intent(ClassB.this, ClassC.class);
            startActivity(intent);
            finish()

        }
        //Here after the comma you specify the amount of time you want the screen to be delayed. 5000 is for 5 seconds.
    }, 5000);
}

//Override onBackPressed method and give it no functionality. This way when the user clicks the back button he will not go back.
public void onBackPressed() {

} }

Upvotes: 2

Jamal S
Jamal S

Reputation: 1835

In Kotlin you can do:

 Handler().postDelayed({
  // Start activity
  startActivity(Intent(this, YourTargetActivity::class.java))
  // terminate this activity(optional)
  finish()
}, 5000)

Upvotes: 0

Related Questions