2

I need to launch an activity telling what activity it must launch after the user has completed a form.

I tried this and it seems correct

new Intent(this, Activity1.class)
     .putExtra("myActivity", Activity2.class);

If I had to make it launch another class I would do

new Intent(this, Activity1.class)
     .putExtra("myActivity", AnotherActivity.class);

Is it the right way?

In Activity1 how should I get the extra argument of .class type?

user3290180
  • 4,260
  • 9
  • 42
  • 77
  • 4
    You don't do that; pass a unique value like a String "ActivityB" or C or integer like 1, 2, 3 then switch when you get the extras to know which activity to launch! – Eenvincible Jul 23 '16 at 08:04
  • you're right , but it seemed like a way to avoid the switch part. – user3290180 Jul 23 '16 at 08:06
  • How many activities do you need to choose from? Maybe from there I can help – Eenvincible Jul 23 '16 at 08:06
  • actually there are two activities – user3290180 Jul 23 '16 at 08:07
  • That should be easy; let me give an answer below for you to try; – Eenvincible Jul 23 '16 at 08:08
  • another problem is that Activity1 is in a library so it doesn't see Activity2 or AnotherActivity, they are in the same module that calls the library – user3290180 Jul 23 '16 at 08:10
  • I don't think that is a problem; just wait a minute for me to finish this part – Eenvincible Jul 23 '16 at 08:11
  • @user3290180 I think what you are doing is wrong, you can pass a class object using intent, not the class itself, An intent is meant for passing objects/data between two components of an application or launch an activity please refer [Intent](https://developer.android.com/guide/components/intents-filters.html) – SaravInfern Jul 23 '16 at 08:16
  • Why? if I could pass a class object then I could also use it as a variable to launch that activity with another intent – user3290180 Jul 23 '16 at 08:18

3 Answers3

2

Pass the Activity's name with the package, then get the desired class through reflection.

Passing the data:

new Intent(this, Activity1.class)
     .putExtra("myActivity", "com.mypackage.activity.Activity2");

Getting the desired activity:

String desiredActivityName = getIntent().getExtras().getString("myActivity");
Class<? extends Activity> targetActivity = Class.forName(desiredActivityName).asSubclass(Activity.class);
Intent launchIntent = new Intent(this, targetActivity);
Ricardo A.
  • 685
  • 2
  • 8
  • 35
0

Try the following sample code:

private static final int LAUNCH_A = 100;
private static final int LAUNCH_B = 200;

//launch activity here with extras to decide which other activity to launch next

Intent intent = new Intent(this, Activity1.class);

//Now decide here, which activity you want to launch in the next one

if(shouldLaunchA){
   intent.putExtra("LAUNCH_TARGET", LAUNCH_A); 
}else{
   intent.putExtra("LAUNCH_TARGET", LAUNCH_B);
}

startActivity(intent);

Now, in your Activity 1, you can get the extras and switch on them:

Intent intent = getIntent();

// you could place this as a class level variable:
int launchTarget = intent.getExtras().getInt("LAUNCH_TARGET");

switch(launchTarget){
   case 100:
      //Launch activity A
      break;
   case 200:
      //launch activity B
      break;
}

That is all you need to get this working!

Like I hinted above, you should set the launchTarget variable as a class level variable so you can access it once the user has enter data to your EditText fields and clicked a button to submit!

Good luck and let me know if you need further assistance!

Eenvincible
  • 5,641
  • 2
  • 27
  • 46
-1

Why do you ant to launch an activity which inturn launches other activity. Instead, use if or switch conditions to lauch different activities directly

Create the following class

public void startIntent(Class c){
startActivity(new Intent(youActivity.this,c)
}

If you want to use if statements to launch different activities

if(your condition){
startIntent(Activity1.class)
}
if(another condition){
startIntent(anotherActivity.class)
}
Vidya Sagar
  • 170
  • 1
  • 16
  • Yes, I could make a class where I set the class object I need to launch after Activity1 as completed the task, then this will get it from the same class – user3290180 Jul 23 '16 at 08:21