47

It seems like every example I can find of switching between activities involves creating an Intent and passing in the context of a View via an OnClickListener associated with a button.

But what if you just decide you need to switch activities? In my case, a preference value is causing an Activity switch.

How do you create an Intent that can cause an Activity switch without an associated OnClickListener?

Cœur
  • 37,241
  • 25
  • 195
  • 267
dicroce
  • 45,396
  • 28
  • 101
  • 140

8 Answers8

91

This should do it for you:

Intent myIntent = new Intent(this, MyActivityName.class);
startActivity(myIntent);

You can call that from anywhere in your current activity.

Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
Chris Thompson
  • 35,167
  • 12
  • 80
  • 109
  • 1
    is it possible to pass activity name as a String? – mhshams Aug 28 '10 at 16:28
  • 4
    @Mohammad Not according to any of the public signatures I've seen, but what you could do is use `Class.forName(fully qualified class name)` to return a `Class` object and then pass that `Class` object to the `Intent` constructor. – Chris Thompson Aug 28 '10 at 16:31
  • 1
    create a method that reads user input and uses it's substring as a parameter to call another class through reflection would be a terrible idea – Rafael Lima May 05 '18 at 17:11
9

It depends where you want to start the new activity in the code. You need the access to a Context reference to start a new activity( For example: onPostExecute in AsyncTask). Please have a look at this.

Even though it is basically this.

 Intent myIntent = new Intent(this, ActivityName.class);
 startActivity(myIntent);

It can be something like this as well

Intent myIntent = new Intent(context, ActivityName.class);
context.startActivity(myIntent);
Community
  • 1
  • 1
diyoda_
  • 5,274
  • 8
  • 57
  • 89
  • 1
    I found funny how you made the exact same typo as the accepted answer with the class name `AvitivityName` ;) You could have just point that in a comment you know :) – AxelH Nov 11 '16 at 13:37
5

I have the shortest Version

startActivity(new Intent(CurrentActivity.this,ActivityYouWantToOpen.class));
Zar E Ahmer
  • 33,936
  • 20
  • 234
  • 300
5
startActivity (new Intent (Thisactivity.this, Nextactivity.class));

Don't forget to add activity to your manifest

<Activity android:name=".NextActivity>
Yamikani Sita
  • 51
  • 1
  • 5
3

when ever u want to switch activity . u can call these code .

Intent intent = new Intent(this, MyActivity.class);
startActivity(intent);

You can write this code in PreferenceChangeListener.

Srinivas
  • 1,688
  • 8
  • 30
  • 51
2

You can create intent in the main activity like this

Intent intent = new Intent(FirstActivity.this, second.class);
startActivity(intent);

If you are waiting for result from the second then you should use

StartActivityforresult(intent,request code).

Request code can be any integer.

xan
  • 7,440
  • 8
  • 43
  • 65
Rakesh Gondaliya
  • 1,050
  • 3
  • 25
  • 42
1

Use PreferenceChangeListener :)

Konstantin Burov
  • 68,980
  • 16
  • 115
  • 93
1

Firstly you need to create UI for a button by using layout intro_activity_1.XML file. After that set id for button group using android:id="@+id/button"

Example:

intro_activity_1.xml

<Button    android:id="@+id/button"    
android:layout_width="wrap_content"    
android:layout_height="wrap_content"    
android:layout_weight="1"    
android:background="@android:color/transparent"    
android:text="NEXT" />

Now change your java class of first activity. In this example, we change java file of IntroActivity1.java

Example:

IntroActivity1.java

//header, import and package data


public class IntroActivity1 extends AppCompatActivity {


    Button next_btn;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.intro_activity_1); 

           next_btn=(Button)findViewById(R.id.button);//button class

           next_btn.setOnClickListener(new View.OnClickListener(){
   public void onClick(View arg0){
       //Start new activity class
              Intent myIntent=new Intent(IntroActivity1.this,IntroActivity2.class);
             startActivity(myIntent);
      }
  });
}

For More details about activity changer visit : https://answerdone.blogspot.com/2018/01/how-to-change-new-activity-in-android.html

Ganesh Garad
  • 381
  • 2
  • 6