Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to call the start activity from one java class

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?

like image 647
Saravanan Avatar asked Feb 19 '14 17:02

Saravanan


People also ask

How do you call an activity in Java?

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 .

Which method is use for start activity?

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.

How do you check which intent started the activity?

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.


2 Answers

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()
like image 195
Zohab Ali Avatar answered Sep 18 '22 15:09

Zohab Ali


Start new Activity in a single line of code.

startActivity(new Intent(this, ActivityName.class));
like image 29
Mukesh Kumar Patel Avatar answered Sep 20 '22 15:09

Mukesh Kumar Patel