Home>

I want to create a behavior that transitions to another screen after touching the title screen

Applicable source code
[MainActivity.kt]
package com.example.test
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
class MainActivity: AppCompatActivity () {
    // Title screen display
    override fun onCreate (savedInstanceState: Bundle?) {
        super.onCreate (savedInstanceState)
        setContentView (R.layout.activity_title)
    }
    // Touch the title screen and move to the main screen
        @Override
    public boolean onTouchEvent (MotionEvent event) {
        Intent intent = new Intent (this, MainActivity.class);
        startActivity (intent);
        return true;
    }
}

Create a title screen with the name activity_title,
It is described that transition to the activity_main screen when the title screen is touched, but it does not work. .
I ’m trying my first app, but it ’s not going well. .

Supplemental information (FW/tool version etc.)

Please provide more detailed information here.

  • Answer # 1

    The program is structured as follows

    Program structure
    [MainActivity]-[activity_main]

    [SubActivity]-[activity_sub]

    MainActivity screen is activity_main,
    SubActivity screen as activity_sub
    Flying from MainActivity to SubActivity

    override fun onTouchEvent (event: MotionEvent?): Boolean {
           // 1. Recommended
            val intent = Intent (this, SubActivity :: class.java)
            startActivity (intent)
    // // This is acceptable
           //setContentView(R.layout.activity_sub)
            return true
        }

    You can also open activity_sub with MainActivity.
    Please switch to comment