'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?
Solution 1:[1]
To start an Activity
you need a Context
.
The method startActivity(Intent intent)
is inherited from the Context
class. As it can be seen in the Documentation
Also an explicit Intent
itself needs a Context
in its constructor.
Intent(Context packageContext, Class<?> cls)
As Activity extends Context
and you've extended Activity you can use your own class as Context.
And thus simply call
void method() {
startActivity(new Intent(this, ActivityName.class));
}
If you do not want to extend Activity you can pass the Context as an argument.
public static void startActivity(Context context) {
context.startActivity(new Intent(context, ActivityName.class));
}
BASIC IMPLEMENTATION
public class ActivityA extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate();
}
public void onClick(View view) {
ActivityStarter.startActivityB(this);
}
}
public class ActivityB extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate();
}
}
public class ActivityStarter {
public static void startActivityB(Context context) {
Intent intent = new Intent(context, ActivityB.class);
intent.putExtra("sub1","chemistry");
context.startActivity(intent);
}
}
Solution 2:[2]
Yeah, it is possible. And extending the activity is not necessary too. Let's name our custom class as Custom.java
Let's assume that we need to start the NewActivity
class when method()
is called in our Custom class.
STEP 1:
As EndZeit pointed out, you need context of your MainActivity class in your Custom.java class to start a new activity from that point.
Custom custom = new Custom(MainActivity.this);
STEP 2:
In the custom class, make a constructor and receive the context passed:
Private Context context;
public Custom (Context context) {
this.context = context;
}
STEP 3:
Now, start the new activity:
public void method() {
Intent intent = new Intent(context, NewActivity.class);
context.startActivity(intent);
}
You're done :)
Solution 3:[3]
Just use the following code from any Java class:
Intent i = new Intent(getContext(),TargetActivity.class);
getContext().startActivity(i);
Solution 4:[4]
Change this line:
Intent t= new Intent(testclass.this,Subject.class);
to:
Intent t= new Intent(testclass.this,subactivity.class);
Also, put a reference to subactivity in your Manifest file
Something like:
<activity android:name="com.example.app.subactivity" />
Solution 5:[5]
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()
Solution 6:[6]
Start new Activity in a single line of code.
startActivity(new Intent(this, ActivityName.class));
Solution 7:[7]
When a class extens Activity
then turns to Activity class. So, your both classes, subactivity
and testclass
, activity class.
Yeah, you can start a activity from another class.
Follow the below tutorial link...you will get to know how to start an activity from another activity
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Amit Vaghela |
Solution 2 | Community |
Solution 3 | JJJ |
Solution 4 | Phantômaxx |
Solution 5 | Zohab Ali |
Solution 6 | Mukesh Kumar Patel |
Solution 7 |