How to Make Android Apps 5

Passing Data between ActivitiesIn this tutorial I’ll show you how to create multiple Android Activities and then how you’d go about passing data between Android Activities. I also cover Intents.

I’ll specifically cover creating multiple Activity layouts, setting up the manifest file, sending data to an Activity when you open it, retrieving data when an Activity closes and what Intents are. All of the code can be found below and it is heavily commented to help you learn.

If you like videos like this, it helps others find it if you share on Google Plus with a click here [googleplusone]

Code from the Video

second_layout.xml

activity_main.xml

MainActivity.java

SecondScreen.java

AndroidManifest.xml

18 thoughts on “How to Make Android Apps 5”

  1. Hey Derek.

    First of all: thanks for this tutorial. It’s really great and is helping me a lot.

    After watching this video and the next one, I thought it would be great for practice switching activitis by making an app that works like this: the main screen have a button, when you click this button you go to other activity and you can write a name, and then you gou to another one so you can press the age and then, finally, you go to the last one so you can type your height.
    When you click the last button, it creates an object and sends it to the first activity that displays it in the screen.

    It worked great, but I’m not sure I’m doing it the most efficient way.

    In the last activity, I typed setResult(..) and sent the data to the last screen, and did it with all of them until the data arrived in the main activity. I uploaded it on pastebin so you can see it if you don’t get it (I’m bad at explaining things in english): http://pastebin.com/R8XDEzrV

    So… Am I doing it the right way?

    1. You’re very welcome Matheus. Yes that works as well. I’m not optimizing everything so that I can really focus in on the main concepts. As the tutorial continues I’ll make much more optimized apps.

  2. Love the tutorials! Just started following along in Eclipse, when I tried to install Android Studio I had so many problems over 2 days solid I just went back to Eclipse! (acronym – AS+ Android Studio Supposedly) for example:
    1. current AS+ version is beta, nothing like your tut.
    2. SDK mgr, all sorts of problems ???
    3. AVD mgr didn’t recognize my device but adb.exe did.
    4. a lot of install content errors recommending to deactivate anti-virus protection even after completely uninstalled.
    5. on & on & on for 2 days
    ** I don’t think it uninstalls cleanly, leaves a lot of directories and sdk versions.
    Any suggestions ?
    Windows 8.1 (ugh!) 64-bit, i5 duo-core 16 gb RAM

    1. Thank you 🙂 I’m doing my best to make the tutorial work with Eclipse so if you prefer it stick with it. I did cover some common ways to solve AS problems in part 6 of this tutorial series. I hope that helps

  3. Derek, let me get this straight. Every screen requires it’s own XML file, but they can all use strings.xml for global XML settings?

  4. Hi Derek,

    How are you? I hope your doing well…

    First of all: thanks alot for this tutorial. But it would be great if you can upload another video on how to launch another activity from button or just a multiple activities without data transfering.

    Thanks,
    And keep it up your doing a very job.

      1. The code is same as yours , very very Thank You ! You are so nice ! ; )

        11-10 01:51:10.339 30651-30651/com.lvianghan.switchingscreens2 E/AndroidRuntime﹕ FATAL EXCEPTION: main
        Process: com.lvianghan.switchingscreens2, PID: 30651
        java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=0, data=null} to activity {com.lvianghan.switchingscreens2/com.lvianghan.switchingscreens2.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method ‘java.lang.String android.content.Intent.getStringExtra(java.lang.String)’ on a null object reference
        at android.app.ActivityThread.deliverResults(ActivityThread.java:3432)
        at android.app.ActivityThread.handleSendResult(ActivityThread.java:3475)
        at android.app.ActivityThread.access$1300(ActivityThread.java:139)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1258)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5086)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
        Caused by: java.lang.NullPointerException: Attempt to invoke virtual method ‘java.lang.String android.content.Intent.getStringExtra(java.lang.String)’ on a null object reference
        at com.lvianghan.switchingscreens2.MainActivity.onActivityResult(MainActivity.java:76)
        at android.app.Activity.dispatchActivityResult(Activity.java:5446)
        at android.app.ActivityThread.deliverResults(ActivityThread.java:3428)
                    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3475)
                    at android.app.ActivityThread.access$1300(ActivityThread.java:139)
                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1258)
                    at android.os.Handler.dispatchMessage(Handler.java:102)
                    at android.os.Looper.loop(Looper.java:136)
                    at android.app.ActivityThread.main(ActivityThread.java:5086)
                    at java.lang.reflect.Method.invoke(Native Method)
                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)

  5. Hey Derek, I was following your tutorial word for word and I seem to keep getting an error you’re not. I’ve even looked through your code and everything seems to be exactly the same. It’s at the very end in the MainActivity.java the very bottom where we are doing the text views.

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    TextView usersNameMessage = (TextView)
    findViewById(R.id.users_name_message);

    String nameSentBack = data.getStringExtra(“UsersName”);

    usersNameMessage.append(” ” + nameSentBack);

    The “users_name_message” bit is in red and telling me that it cannot resolve the symbol. Sorry if that doesn’t make sense

Leave a Reply

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