Skip to content

aviyehuda.com

Menu
  • Open Source
  • Android
  • Java
  • Others
  • Contact Me
  • About Me
Menu

Android – Opening a new screen

Posted on 28/11/2010
android   Opening a new screen is fairly easy in android. This post will show you almost all you need to know about switching back and forth between screens in your Android application.

Add all activities to manifest

  • Each screen is usually an Activity.
  • Each activity should be mentioned in the manifest file.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
	package="com.aviyehuda.puzzle" android:versionCode="1"
	android:versionName="1.0">
	<application android:icon="@drawable/icon" 
			android:label="@string/app_name">
		<activity android:name="MyActivity">

			<intent-filter>
				<action android:name="android.intent.action.MAIN" />
				<category android:name="android.intent.category.LAUNCHER" />
			</intent-filter>

		</activity>

		<activity android:name=".MyActivity2" >

		</activity>

    
</application>
<uses-sdk android:minSdkVersion="6" />
</manifest> 

The simple use

Intent in = new Intent(MyActivity.this, MyActivity2.class);
startActivity(in);
  • Bear in mind that every time you will call the function startActivity(intent) a new instance of MyActivity2 will be created.

Passing a parameter

  • In most times you need to pass a parameter to the new screen. You do that by using the function putExtra().
  • This function
Intent in = new Intent(myactivity.this, MyActivity2.class);
in.putExtra("myKey", "new1");
startActivity(in);

Receiving a parameter

  • And of course you will need to receive that parameter on the other side.
String s= getIntent().getExtras().get("myKey").toString()

Going back

  • If you need to go back to the previous screen and terminate the current screen, than all you have to do is to use the function finish() from anywhere in the new Activity.
finish();

Open for result

  • When you need to open a new screen and get result back from it, than use the function startActivityForResult() instead of startActivity()
Intent in = new Intent(myactivity.this, MyActivity2.class);
startActivityForResult(in, 0);

Returning a result

Intent in = getIntent();
in.putExtra("result", "some parameter");
setResult(RESULT_OK,in);
finish();

Getting the result from the new activity

@Override
    protected void onActivityResult(int requestCode,
                                     int resultCode, Intent data) {
    	super.onActivityResult(requestCode, resultCode, data);
    	
    	Log.d("Result",""+data.getExtras().get("result"));
    } 

1 thought on “Android – Opening a new screen”

  1. Lebone says:
    11/06/2013 at 23:47

    Man, this was very helpfull. Thanks a million for not being selfish and sharing this info.

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *


About Me

REFCARD – Code Gems for Android Developers

Categories

  • Android
  • AWS
  • AWS EMR
  • bluetooth
  • Chrome extension
  • ClientSide
  • Clover
  • Coding Coventions
  • Data Lake
  • General
  • GreaseMonkey
  • Hacks
  • hibernate
  • hibernate validator
  • HTML5
  • HtmlUnit
  • Image Manipulation
  • Java
  • Java Technologies
  • JavaScript
  • Java_Mail
  • JEE/Network
  • Job searching
  • Open Source
  • Pivot
  • projects
  • Pure Java
  • software
  • Spark
  • Trivia
  • Web development

Archives

  • March 2022 (1)
  • January 2022 (1)
  • January 2021 (1)
  • December 2018 (1)
  • August 2018 (1)
  • October 2013 (1)
  • March 2013 (1)
  • January 2013 (2)
  • July 2012 (1)
  • April 2012 (1)
  • March 2012 (1)
  • December 2011 (1)
  • July 2011 (1)
  • June 2011 (1)
  • May 2011 (2)
  • January 2011 (1)
  • December 2010 (1)
  • November 2010 (3)
  • October 2010 (4)
  • July 2010 (1)
  • April 2010 (2)
  • March 2010 (1)
  • February 2010 (2)
  • January 2010 (5)
  • December 2009 (10)
  • September 2009 (1)
 RSS Feed
1d96f52e7159fe09c7a3dd2a9816d166-332
©2023 aviyehuda.com | Design: Newspaperly WordPress Theme