Switching Activities: Android Programming @DrBFraser
Switching Activities: Android Programming  @DrBFraser
Uploaded February 2013 | Updated November 2026, 3 hours ago
Updated version of video using Android Studios found here: youtu.be/ToOX2OTCYAk

Guide/demonstration to switching between activities in an Android program.
Steps listed below in the description.

Link to previous demo for creating a button:
youtube.com/watch?v=Uen8B9p05tI


Steps:
==========
1. Create the layout
- Create a new layout file under res/layout
- Named: second.xml
- Make it show something new (text field, icon...)

2. Create new Java class
- Create a new Java class: SecondScreen
- Override the onCreate():
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
}

3. Add activity to manifest
- Application tab, add new "Application Nodes"
- Add, "Create a new element at the TOP LEVEL", Activity
- Set name: Select java class from above.
- Set Label (if desired)

4. Java code to switch activities
---------------------------
startActivity(new Intent( [Current Activity's 'this'], [New Activity's Java].class));

Ex:
startActivity(new Intent(InClassDemoApp1.this, SecondScreen.class));

- Suggestion: Put launch code inside button callback:

Button daClicker = (Button) findViewById(R.id.button1);
daClicker.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, SecondScreen.class));
}
});

5. Activity Stack
=====================
- Loading an Activity are pushed on the Activity *Stack*
- Activity's finish() method pops it
Returns to previous activity on stack.
- Must use correctly!

- Can move on and kill current activity:
startActivity(...);
finish();


6. Troubleshooting:
---------------------------
- Demonstrate what happens with the activity being in the manifest.
* View LogCat via DDMS:
...android.content.ActivityNotFoundException: Unable to find explicit activity class ...; have you declared this activity in your AndroidManifest.xml?
Switching Activities: Android ProgrammingSpring Boot: Getting Started in IntelliJGitLab Workflow: Merge Request with Android Studio / IntelliJRead CSV Resource File: Android ProgrammingCreating a Pie Graph: Android ProgrammingGSON: LocalDate AdapterGraphical Cross Debugging in Eclipse: Embedded LinuxGoogle Maps API: Android ProgrammingGit Clone in Android Studio: Android ProgrammingDownload XML from a Web Server: Android ProgrammingCross Debugging with GDB: Embedded LinuxFork and Exec Linux Programming
Brian Fraser |

Switching Activities: Android Programming

SHARE TO X SHARE TO REDDIT SHARE TO FACEBOOK WALLPAPER