-3

I am writing an android app that has both, Java and Kotlin activities. I wished to Move from Kotlin to Java, that is done, now I wish to move back to Kotlin activity, I can't find specific code for that.

My app name is starter and the Kotlin activity is MainActivity.kt and the java class is face_detect.java. I have already tried: `

btn2.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v) {
                Intent i = new Intent(this, MainActivity.class);
                startActivity(i);
            }
        });

`

I am expecting to click the button on face_detect.java and that would take me to MainActivity.kt

5 Answers5

3

this inside an anonymous class represents that class. Here you need the reference of the Activity in which the button is added.

Change to

Intent i = new Intent(MyCurrentActivity.this, MainActivity.class); 

and replace MyCurrentActivity with your current activity name.

Rohit5k2
  • 17,948
  • 8
  • 45
  • 57
0

Intent intent = new Intent(getActivity(), KotlinActivity.class);

startActivity(intent);

Replace the KotlinActivity.class with your activity name.

Geekfreak
  • 13
  • 10
0

Try to replace Intent i = new Intent(this, MainActivity.class); by Intent i = new Intent(this, MainActivity::class.java);

Ady
  • 230
  • 3
  • 16
0

Possible duplicate of Kotlin Android start new activity

Also inside click listener instead of passing "this" ,pass my current activity.this

Ashok Kumar
  • 1,226
  • 1
  • 10
  • 14
0

Please try this code. write normal intent .

Intent intent=new Intent(CurrentActivtiy.this,otherActivity.class);
startActivity(inent)
Raju Tukadiya
  • 229
  • 1
  • 8