Monday, October 13, 2014

Coursera Android Intents, Permissions, Fragement class (week 3)

Week 2 touched on Intents, now in week 3 Intents are covered in more detail.

This weeks code examples quick reference:
HelloAndroidWithLogin
PermissionExampleBoom
PermissionExampleBoomUser
FragmentDynamicLayout
FragmentDynamicLayoutWithActionBar
FragmentProgrammaticLayout
FragmentQuoteViewerWithActivity
FragmentStaticConfigLayout
FragmentStaticLayout


1) Intents Part 1 slides & presentation
http://developer.android.com/training/basics/intents/index.html
intent is a data struct that represents:
- an operation to be performed
- event that has occurred in system and that you want to notify other components about
Language for specifying operations that you want to have performed
Constructed by Activity that wants some work done, Received by Activity that performs the work.

action is string that names operation to be performed such as action_edit or action_sync (set by setter or constructor of an intent)
data associated with event that is formatted as URI such as URI.parse()  (set by setter or constructor of an intent)
category is additional information about what the event can handle such as category_browsable or category_launchable
type is the mime type such as image/type (here is method for setting data and type)
component indicates the target activity passing in context and the class (see setClass, setComponent, setClassName)
extras has additional info in key value pair such as Intent.extra_email key with putExtra method
flags indicate how intent should be handled such as flag_activity_no_history or flag_debug_log_resolution which provides more information about how Android determines which Activities can respond to a given Intent.

Intents Part 2 presentation


target activity explicitly named by component
https://github.com/aporter/coursera-android/tree/master/Examples/HelloAndroidWithLogin
LoginScreen
LoginScreen.java:
   Intent helloAndroidIntent = new Intent(LoginScreen.this, HelloAndroid.class);
   startActivity(helloAndroidIntent);

also its possible to start an activity and receive a result back (example is start a camera app and receive the captured photo as a result)

Send the response back after a startActivityForResult

   Intent returnIntent = new Intent(); 
  returnIntent.putExtra("result",result); 
  setResult(RESULT_OK,returnIntent); 
  finish();


implicitly by Intent used properties on device

http://developer.android.com/training/basics/intents/sending.html

intent resolution - not explicit so android tries to find activities that match the intent. looks at :

  1. action
  2. data
  3. category


provide intent describing operation and intent filters describing operations the activity can handle via the manifest file or programatically
http://developer.android.com/guide/components/intents-filters.html

to receive an implicit intent must specify android.intent.category.DEFAULT

-1000 to 1000  priority with matching implicit intents

% adb shell dumpsys package


2) Android Permissions slides & Presentation


Android protects applications, resources & data with permissions Permissions are represented as strings Applications specify permissions they use through a tag Users must accept these permissions .

Permissions they use themselves,  or require of others.

specify permissions that the device's user must grant to the application before that application can run on the user's device:
http://developer.android.com/guide/topics/manifest/uses-permission-element.html
<uses-permission>
android.permission.CAMERA

http://developer.android.com/reference/android/Manifest.permission.html

Bookmarks permission

contacts list is private data
<uses-permission android:name="android.permission.READ_CONTACTS" >
in AndroidManifest.xml

In addition to using permissions, an app can define and enforce their own permissions. you may not want any application to use yours. Other applications must be problematically granted your permission otherwise wont be able to use the application.

https://github.com/aporter/coursera-android/tree/master/Examples/PermissionExampleBoom : simple app that has a dangerous action (Boom goes Dynamite).

Define a permission: <permission android:name="course.examples.permissionexample.BOOM_PERM" android:description="@string/boom_perm_string" android:label="@string/boom_permission_label_string"> </permission>

https://github.com/aporter/coursera-android/tree/master/Examples/PermissionExampleBoomUser requires 
<uses-permission android:name="course.examples.permissionexample.BOOM_PERM" /> to run

can set permission to individual components known as Activity Permissions -precedence over app level permissions 

checked with context on services start,stop,bind 
broadcast receiver permissions
content provider permissions says who can read and write data

3) Android Fragments Part 1 slides & Presentation


Fragments added for large devices in 3.0  for tablets. Fragments represent portion of UI within activity. In other words, an activity can host multiple fragments. Also, a single fragment can be reused over several activities.

Fragment life cycle coordinated with activity but has its own callbacks too

Running or Resumed (fragment is visible within running activity)
Paused (another activity visible and in foreground)
Sopped (fragment not visible)

Fragment Lifecycles
onAttach() - Fragment is first attached to its activity
onCreate() initializes fragment, doesn't create UI component though
onCreateView() sets up a returns a View (user interface) that is given to Activity
onActivityCreated() Fragment has been installed in Activity
OnStart() - coordinated with the activities OnStart() method
OnResume() - about to become visible and ready for user interation
OnPause() - losing focus
OnStop() - no longer visible
OnDestroyView()  clean up resources
OnDestroy()  no longer in use
OnDetach() - fragment no longer attached to activity





Android Fragments Part 2 Presentation


statically added or fragment manager programatically

1) in layout and set in setContentView

2) programmatically via the FragmentManager 

onCreateView returns the inflated view thats at root of the fragment layout, its is then added to the associated activity

FragmentStaticLayout FragmentStaticLayout example:

main.xml

<fragment android:id="@+id/titles" android:layout_width="0px" android:layout_height="match_parent" android:layout_weight="1" class="course.examples.Fragments.StaticLayout.TitlesFragment" /> 

<fragment android:id="@+id/details" android:layout_width="0px" android:layout_height="match_parent" android:layout_weight="2" class="course.examples.Fragments.StaticLayout.QuotesFragment" />

QuoteViewerActivity

   setContentView(R.layout.main);

QuoteFragment extends Fragment

  like a setContentView:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.quote_fragment, container, false);
}

Can add fragment dynamically on fly while its running by
1) get reference to fragment manager, 2) begin fragment transaction 3) add fragment to activity 4)commit fragment transaction
FragmentProgrammaticLayout

another example:


// Get a reference to the FragmentManager FragmentManager  mFragmentManager = getFragmentManager(); // Start a new FragmentTransaction FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction(); // Add the TitleFragment to the layout from main_activity.xml fragmentTransaction.add(R.id.fragment_container, mFriendsFragment); // Commit the FragmentTransaction fragmentTransaction.commit();

After commit above the fragment methods get called:

  • public void onAttach(Activity activity) {
  • public void onCreate(Bundle savedInstanceState) {
  • public void onActivityCreated(Bundle savedInstanceState) {



 

Subviews are FrameLayout instead of fragments here.

https://github.com/aporter/coursera-android/blob/master/Examples/FragmentProgrammaticLayout/src/course/examples/Fragments/ProgrammaticLayout/QuoteViewerActivity.java

FragmentManager fragmentManager = getFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager .beginTransaction(); fragmentTransaction.add(R.id.title_frame, new TitlesFragment()); fragmentTransaction.add(R.id.quote_frame, mQuoteFragment); fragmentTransaction.commit();

FragmentDynamicLayout

QuoteViewerActivity

public void onListSelection(int index) { if (!mQuoteFragment.isAdded()) { FragmentTransaction fragmentTransaction = mFragmentManager .beginTransaction(); fragmentTransaction.add(R.id.quote_fragment_container, mQuoteFragment); fragmentTransaction.addToBackStack(null); fragmentTransaction.commit(); mFragmentManager.executePendingTransactions(); } if (mQuoteFragment.getShownIndex() != index) { mQuoteFragment.showIndex(index); } }

config changes

Suppose you have an Activity that hosts a Fragment. This Fragment has invoked the setRetainInstance() method, passing in the parameter true.

setRetainInstance (true) - wont destroy state of fragment on config changes and retain fragment state - onDestroy and OnCreate wont be called if the Activity is later killed and restarted.
FragmentStaticConfigLayout

TitlesFragment.java

setRetainInstance(true);
onActivityCreated method


http://developer.android.com/guide/practices/tablets-and-handsets.html




http://developer.android.com/training/basics/intents/ 
http://developer.android.com/training/basics/intents/filters.html
- action: A string naming the action to perform. Usually one of the platform-defined values such as ACTION_SEND or ACTION_VIEW.
- data:  A description of the data associated with the intent.
- category : provides an additional way to characterize the activity handling the intent, usually related to the user gesture or location from which it's started. There are several different categories supported by the system, but most are rarely used. However, all implicit intents are defined with CATEGORY_DEFAULT by default.

<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="http" />
</intent-filter>



Start a new Activity using this second Intent.

http://developer.android.com/training/basics/intents/sending.html


Uri pageURI = Uri.parse(URL); Intent baseIntent = new Intent(Intent.ACTION_VIEW, pageURI); PackageManager packageManager = getPackageManager(); List<ResolveInfo> activities = packageManager.queryIntentActivities(baseIntent, 0); boolean isIntentSafe = activities.size() > 0;
Multiple applications may be able to handle this Intent App Chooser:

Intent chooserIntent = Intent.createChooser(baseIntent, CHOOSER_TEXT);
startActivity(chooserIntent);


private EditText mEditText;
EditText  extends TextView
-getText()

No comments:

Post a Comment