Is it possible to start an Activty
using an Intent
in a general java
class which extends Activity
?
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Spinner;
import android.app.Activity;
import android.content.Intent;
public class SubActivity extends Activity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
And a general java class like the following
class TestClass extends Activity{
void firstsem(){
Intent t = new Intent(this, SubActivity.class);
t.putExtra("sub1","chemistry");
startActivity(t);
}
}
Is it possible to start an Activity from an general java class? Could anybody show me how to achieve this?
To start an activity, call startActivity() and pass it your Intent . The system receives this call and starts an instance of the Activity specified by the Intent .
1.2. To start an activity, use the method startActivity(intent) . This method is defined on the Context object which Activity extends. The following code demonstrates how you can start another activity via an intent. # Start the activity connect to the # specified class Intent i = new Intent(this, ActivityTwo.
Handle the Intent in Your Activity In order to decide what action to take in your activity, you can read the Intent that was used to start it. As your activity starts, call getIntent() to retrieve the Intent that started the activity.
In Kotlin this is how you can do it
I am asuming that you have context
available
val intent = Intent(context, YourActivity::class.java)
context.startActivity(intent)
(context as Activity).finish()
Start new Activity in a single line of code.
startActivity(new Intent(this, ActivityName.class));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With