DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. Switching Activities in Android

Switching Activities in Android

Madhuka  Udantha user avatar by
Madhuka Udantha
CORE ·
Jun. 18, 14 · Interview
Like (0)
Save
Tweet
Share
4.22K Views

Join the DZone community and get the full member experience.

Join For Free

The following article shows how to create a new activity when the user clicks the button. 

1. Create a new activity

image

2. You can see new UI for 2nd activity and then we change string value as below

image

3. In MainActivity.java we can added below line to switch activity. (there are few ways to achieve it, this only one way)

Intent intent =new Intent(MainActivity.this,SecondActivity.class);2 startActivity(intent);

[Note]


An intent is an abstract description of an operation to be performed. It can be used withstartActivity to launch an Activity, broadcastIntent to send it to any interestedBroadcastReceiver components, and startService(Intent) or bindService(Intent, ServiceConnection, int) to communicate with a background Service.


4. Just run app and see it. (click back button to go to previous activity)



Screenshot_2014-06-15-19-07-14[1]
Screenshot_2014-06-15-19-07-22[1]

Now we will try to pass message from one activity to second activity

[NOTE]

An intent not only allows you to start another activity, but it can carry a bundle of data to the activity as well.

//passing string 
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
intent.putExtra(EXTRA_MESSAGE, name);
startActivity(intent);

Then we will retrieve data

// Get the message from the intent
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

Fragment

A Fragment represents a behavior or a portion of user interface in an Activity. You can combine multiple fragments in a single activity to build a multi-pane UI and reuse a fragment in multiple activities. A fragment must always be embedded in an activity and the fragment's lifecycle is directly affected by the host activity's lifecycle. For example, when the activity is paused, so are all fragments in it, and when the activity is destroyed, so are all fragments. Android introduced fragments in Android 3.0 (API level 11).  Next post will content more about Fragment with some sample coding.

Android (robot) Fragment (logic)

Published at DZone with permission of Madhuka Udantha, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Taming Cloud Costs With Infracost
  • Top 5 PHP REST API Frameworks
  • The Importance of Delegation in Management Teams
  • Beginners’ Guide to Run a Linux Server Securely

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: