Changes

Jump to: navigation, search

Android 6 Explicit Intents – A Worked Example

18 bytes removed, 08:08, 28 January 2016
m
Text replacement - "<google>BUY_ANDROID_STUDIO6</google>" to "<htmlet>androidstudio_a6</htmlet>"
<googlehtmlet>BUY_ANDROID_STUDIO6androidstudio_a6</googlehtmlet>
As covered in [[An Overview and Example of Android 6 Event Handling]], the android:onClick="onClick" resource line in the above XML listing uses an alternative way to handle simple click events without having to implement an event listener in the Java code of the activity. By specifying the onClick event type and the method to be called when such an event is detected, it is quick and easy to wire up basic click event handling on a view. Once the onClick() method has been implemented in the ActivityA.java file, it will be called whenever the button is touched by the user.
<googlehtmlet>BUY_ANDROID_STUDIO6androidstudio_a6</googlehtmlet>
Once the layout is complete, the user interface should resemble that illustrated in Figure 38-1:
<google>ADSDAQBOX_FLOW</google>
</RelativeLayout>
</pre>
<googlehtmlet>BUY_ANDROID_STUDIO6androidstudio_a6</googlehtmlet>
If designing the layout using the Designer tool in Design mode, note that the onClick property on the button view has been configured to call a method named onClick(), the width of the EditText view has been set to 300dp and the views have been assigned IDs button1, TextView1 and editText1. The completed layout should resemble that illustrated in Figure 38-3:
Now that ActivityB is being launched from ActivityA, the next step is to extract the String data value included in the intent and assign it to the TextView object in the ActivityB user interface. This involves adding some code to the onCreate() method of ActivityB in the ActivityB.java source file:
<googlehtmlet>BUY_ANDROID_STUDIO6androidstudio_a6</googlehtmlet>
<pre>
package com.ebookfrenzy.explicitintent;
The code in the above method begins by checking that the request code matches the one used when the intent was issued and ensuring that the activity was successful. The return data is then extracted from the intent and displayed on the TextView object.
<googlehtmlet>BUY_ANDROID_STUDIO6androidstudio_a6</googlehtmlet>
== Returning Data from a Sub-Activity ==
<googlehtmlet>BUY_ANDROID_STUDIO6androidstudio_a6</googlehtmlet>

Navigation menu