70,183 views 233 on YTPak
362 4

Published on 05 Feb 2013 | over 4 years 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:
www.youtube.com/watch


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?

Loading related videos...