MobAppDev (Fall 2013): Inter-Activity Data Transfer with Application Objects, Custom Views

Uploaded on


More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads


Total Views
On Slideshare
From Embeds
Number of Embeds



Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

    No notes for slide


  • 1. MobAppDev Inter-Activity Data Transfer with Application Objects, Custom Views Vladimir Kulyukin
  • 2. Outline ● Review: Getting Results from Started Activities ● Application Objects for Inter-Activity Data Transfers ● Custom Views
  • 3. Review Getting Results from Started Activities
  • 4. Development Steps: Launcher ● The launcher activity (MainActivity) defines request codes (one for factorial computation and one for sum computation); request codes are integer constants ● The launcher activity uses explicit intents to launch FactCalcActivity and SumCalcActivity with startActivityForResult(intent, request_code) ● The launcher activity implements onActivityResult() that uses request codes to and data intents from the launched activities to display results
  • 5. Development Steps: Launched ● The launched activities (FactCalcActivity & SumCalcActivity) – get the intent from the Bundle passed to onCreate() – extract the numerical argument from the intent – run the required calculations – create a data intent – place the result of the calculation into that intent – call setResult() with the request code and the data intent
  • 6. MathIntents App: Sample Screenshots Enter 4 Source code is here
  • 7. MathIntents App: Sample Screenshots Click on RETURN N+ TO MainActivity
  • 8. MathIntents App: Sample Screenshots Click on RETURN N+ TO MainActivity
  • 9. MathIntents App: Sample Screenshots Click on N!
  • 10. MathIntents App: Sample Screenshots Click on RETURN N! TO MainActivity
  • 11. Inter-Activity Data Transfers with Application Objects
  • 12. Application Objects ● All activities within one application share the Application object that each activity can obtain via getApplication() ● An application can extend the Application class and request Android to create the object of that class ● The custom Application subclass can create data structures for each activity can access for inter-activity data transfers
  • 13. Example ● Develop an app that subclasses the Application class (ImageDisplayerApp) to create its own custom Application class ● The custom Application object allows two activities (ImageRequestAct and ImageRequestProcessorAct) to display original or grayscale bitmaps ● ImageRequestAct is the main activity (launcher) ● ImageRequestAct stores its request in ImageDisplayerApp ● ImageRequestProcessorAct retrieves the request and executes it ● LuminosityConverter does the grayscale conversion ● Source code is here
  • 14. Sample Screenshots
  • 15. Code Fragments
  • 16. Customizing Application Class public class ImageDisplayerApp extends Application { protected int mImgId = -1; protected String mImgOper = ""; public ImageDisplayerApp() { mImgId = -1; mImgOper = ""; } public void storeRequest(int img_id, String oper) { mImgId = img_id; mImgOper = oper; } public void removeRequest() { mImgId = -1; mImgOper = ""; } public int getRequestImgId() { return mImgId; } public String getRequestImgOper() { return mImgOper; } }
  • 17. Customizing Application Class <application android:icon="@drawable/rus_uzor" android:label="@string/app_name" android:theme="@style/AppTheme" android:name=".ImageDisplayerApp" > // Activity nodes </application>
  • 18. Accessing Customized App Object public class ImageRequestAct extends Activity { // This is the custom application object. ImageDisplayerApp mApp = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Get the custom app object mApp = (ImageDisplayerApp) this.getApplication(); }
  • 19. How ImageRequestAct Stores Info in Application Object mBtnDisplay.setOnClickListener( new OnClickListener() { @Override public void onClick(View v) { int img_id = new Random().nextInt(10); // explicit use of storeRequest method mApp.storeRequest(img_id, mRes.getString(R.string.display_oper_txt)); startActivity(new Intent(mApp, ImageRequestProcessorAct.class)); }} );
  • 20. mBtnGrayscale.setOnClickListener( new OnClickListener() { @Override public void onClick(View v) { int img_id = new Random().nextInt(10); mApp.storeRequest(img_id, mRes.getString(R.string.grayscale_oper_txt)); startActivity(new Intent(mApp, ImageRequestProcessorAct.class)); } } ); How ImageRequestAct Stores Info in Application Object
  • 21. ImageDisplayerApp thisApp = (ImageDisplayerApp) this.getApplication(); mImgView = (ImageView) this.findViewById(; mImgView.setImageDrawable(null); int img_id = thisApp.getRequestImgId(); String img_oper = thisApp.getRequestImgOper(); How ImageRequestProcessorAct Retrieves Info from Application Object
  • 22. Custom Views
  • 23. Custom Views ● Views can be customized to change the look and feel of your applications ● Views can be are typically customized by extending either View or SurfaceView classes ● View is a lightweight solution for 2D graphics ● SurfaceView can be used for 3D graphics
  • 24. Sample Custom View Source code is here
  • 25. Code Fragments
  • 26. Custom View Construction ● What Do We Need To Do To Construct Custom Views? ● Step 1: Specify the mathematicians' names in XML (famous_mathematicians.xml) (Same as in Example 1) ● Step 2: Design and develop a mathematician class ● Step 3: Design and develop a mathematician's view in Java ( and in XML (colors.xml, dimens.xml mathematician_view_1.xml) ● Step 4: Create an ArrayAdapter<Mathematician> ● Step 5: Connect ArrayAdapter<Mathematician> to the activity's ListView
  • 27. Custom View Construction: Step 2 Define a Custom Data Type that Needs a Custom View (See and in ListOfMathematicians Project)
  • 28. Custom View Construction: Step 3 (colors.xml) <?xml version="1.0" encoding="utf-8"?> <resources> <color name="background_clr">#AAFFFF99</color> <color name="line_clr">#FF0000FF</color> <color name="margin_clr">#90FF0000</color> <color name="text_clr">#AA0000FF</color> </resources>
  • 29. Custom View Construction: Step 3 (mathematician_view_1.xml) <?xml version="1.0" encoding="utf-8"?> < xmlns:android="" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="10dp" android:scrollbars="vertical" android:textColor="@color/text_clr" android:textSize="20dp" android:fadingEdge="vertical" />
  • 30. Custom View Construction: Steps 4 & 5 private void populateListViewTwo() { mListOfMathematicians = mRes.getStringArray(R.array.list_of_famous_mathematicians); ArrayList<Mathematician> mathematicians = new ArrayList<Mathematician>(); for(String name : mListOfMathematicians) { String[] fnln = name.trim().split(" "); mathematicians.add(new Mathematician(fnln[0],fnln[1])); } getListView().setAdapter(new ArrayAdapter<Mathematician>(this, R.layout.mathematician_view_1, mathematicians)); }