android tutorial - Start new activity in Android Intent - android studio - android app developement



Start new Activity

You need two activities:

  • CurrentActivity
  • DestinationActivity

In CurrentActivity you have to created an Intent. For that you have to specify two arguments:

  • Context: It's CurrentActivity, because Activity is a subclass of Context.
  • DestinationActivity class
Intent intent = new Intent(Context, DestinationActivity.class);
click below button to copy code from our android learning website - android tutorial - team
  • Then, call startActivity passing the intent created.
startActivity(intent);
click below button to copy code from our android learning website - android tutorial - team
  • Now we have this source:
Intent intent = new Intent(this, DestinationActivity.class);
startActivity(intent);
click below button to copy code from our android learning website - android tutorial - team
  • For example, you can put it in a method an call it when an event ocurred.
void nextActivity(){
     Intent intent = new Intent(this, DestinationActivity.class);
     startActivity(intent);
}

public class CurrentActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.current_activity);
        
        nextActivity();
        finish(); // Finish current activity, if you don't finished it, the current activity will be in background. You can finish it then.
    }
}
click below button to copy code from our android learning website - android tutorial - team

Related Searches to Start new activity in Android Intent