How can I start a new android activity using class name in a string?
Here is a code by which you can start activity using the name of the activity
String activityToStart = "com.example.MainActivity";
try {
Class<?> c = Class.forName(activityToStart);
Intent intent = new Intent(this, c);
startActivity(intent);
} catch (ClassNotFoundException ignored) {
}
EDIT
Here class name will be full name of the class with the package name.
For example if your package name will be x.y.z
and if you have Activity name called A
then the full name of the Activity A
will be x.y.z.A
.
Android Intents: Start activity using class name from another app that has the same sharedUserId
Its pretty easy since you have the sharedUserId set.
Intent res = new Intent();
String mPackage = "com.your.package";
String mClass = ".actYouAreLaunching";
res.setComponent(new ComponentName(mPackage,mPackage+mClass));
startActivity(res);
And that's all there is to it. You can add intent extras like you normally would.
Intent and start activity from string
Try this:startActivity(this, Class.forName(yourStringClass));
Open a new activity from a class string. Class null
When you use Class.forName(className)
method, className
has to be constructed from your package name and your Class
name without the .class
extension.
For ro.movieapp.activities.MovieActivity.class
your call should be something like this: Class.forName("ro.movieapp.activities.MovieActivity")
.
That being said, it is a really bad idea to do it because it's an easy source of bugs (your question is a good example). You should keep a reference to your activity's Class
inside your Movie
object.
My suggestion is to do something like this:
class Movie {
private Class<?> activityClass;
public <C extends AppCompatActivity> void setActivityClass(Class<C> activityClass) {
this.activityClass = activityClass;
}
@SuppressWarnings("unchecked")
public <C extends AppCompatActivity> Class<C> getActivityClass() {
return (Class<C>) activityClass;
}
}
...
final Movie movie = new Movie();
movie.setActivityClass(MovieActivity.class);
...
final Intent intent = new Intent(getActivity(), movie.getActivityClass());
startActivity(intent);
Start new activity getting the class by a String
You can load the class using reflection, e.g.:
public startNewActivity(String activityName){
try {
Class<?> clazz = Class.forName(getPackageName() +"." +activityName);
Intent myActivity = new Intent(CurrentClass.this, clazz);
startActivity(myActivity);
} catch (Exception e) {
Log.e("TAG","Error: " +e.getStackTrace());
}
}
If you provide the full class name (including package name) you don't need the getPackageName()
call.
Hope this helps .. Cheers!
Start Activity with New Intent using dynamic class name
You can use java's reflection methods:
Class activityClass = Class.forName("com.something.Zero");
StartActivity from string
Assuming this is the Activity that you are trying to start:
namespace SushiHangover
{
[Activity(Name = "Resources.biblioteka.SomeActivity", Label = "SomeActivity")]
public class SomeActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
}
}
}
Using the .Net Namespace/Class Name:
var intent = new Intent(this, Type.GetType("SushiHangover.SomeActivity"));
StartActivity(intent);
Using the Java Package/Class Name:
var intent = new Intent(this, Java.Lang.Class.ForName("Resources.biblioteka.SomeActivity"));
StartActivity(intent);
Related Topics
How to Change the Edittext Text Without Triggering the Text Watcher
Method Called After Release() Exception Unable to Resume with Android Camera
Automatically Log Android Lifecycle Events Using Activitylifecyclecallbacks
Libavcodec.So: Has Text Relocations
Onintercepttouchevent Only Gets Action_Down
Httpurlconnection.Getresponsecode() Returns -1 on Second Invocation
How to Transform Currenttimemillis to a Readable Date Format
Center Message in Android Dialog Box
Android Gives Error "Cannot Fit Requested Classes in a Single Dex File"
Preload Multiple Images with Glide
Listview Selection Remains Persistent After Exiting Choice Mode
Problems Importing Project into Android Studio Regarding Actionbarsherlock
Using Httpclient and Httppost in Android with Post Parameters
How to Use Tablayout with Viewpager2 in Android
Android Intent for Sending Email with Attachment
How to Move/Rename File from Internal App Storage to External Storage on Android