Presentation is loading. Please wait.

Presentation is loading. Please wait.

School of Engineering and Information and Communication Technology KIT305/KIT607 Mobile Application Development Android OS –Permissions (cont.), Fragments,

Similar presentations


Presentation on theme: "School of Engineering and Information and Communication Technology KIT305/KIT607 Mobile Application Development Android OS –Permissions (cont.), Fragments,"— Presentation transcript:

1 School of Engineering and Information and Communication Technology KIT305/KIT607 Mobile Application Development Android OS –Permissions (cont.), Fragments, Saving State, and Material Design Dr. Rainer Wasinger, Dr. James Montgomery School of Engineering and ICT

2 School of Engineering and Information and Communication Technology System Permissions (cont.) If your app needs a dangerous permission, you must check whether you have that permission every time you perform an operation that requires that permission. Example: To request dangerous permissions, we need to call additional methods like: –requestPermissions(). 2 int permissionCheck = ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.WRITE_CALENDAR);

3 School of Engineering and Information and Communication Technology System Permissions (cont.) To request dangerous permissions, we need to call the method: –requestPermissions(). 3 if (ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) { if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity, Manifest.permission.READ_CONTACTS)) { } else { ActivityCompat.requestPermissions(thisActivity, new String[]{Manifest.permission.READ_CONTACTS}, MY_PERMISSIONS_REQUEST_READ_CONTACTS); }

4 School of Engineering and Information and Communication Technology System Permissions (cont.) When the user responds to your request, the system invokes the onRequestPermissionsResult() callback. 4 @Override public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { switch (requestCode) { case MY_PERMISSIONS_REQUEST_READ_CONTACTS: { // If request is cancelled, the result arrays are empty. if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { // permission was granted, so do the task you need to do. } else { // permission denied, so disable the // functionality that depends on this permission. } return; } // other 'case' lines to check for other permissions }

5 School of Engineering and Information and Communication Technology Designing for multiple form factors Android 3.0 introduced Android for Tablets –Android tablet screens typically range from 7” to 12” diagonally. –Similar to responsive web design, one needs to consider how the app should be presented based on different available screen widths. –The goal is to design your app once and optimize the design for tablets and older versions of Android (i.e. not separate apps). 5

6 School of Engineering and Information and Communication Technology Fragments Fragments are an optional layer that can be put between activities and widgets to help reconfigure the app to support both large screens (e.g. tablets) and small screens (e.g. phones). –They were first introduced in Android 3.0, but can support devices down to Android 1.6 through the backward compatibility support libraries. –By dividing the layout of an activity into fragments, we can modify the activity’s appearance at runtime. 6

7 School of Engineering and Information and Communication Technology Fragments Here is an example showing how two UI modules can be combined into one activity on a tablet, but split into two activities for a phone. 7

8 School of Engineering and Information and Communication Technology Fragments Steps required to implement Fragments: 1.Create a Fragment class 2.For large displays: Add a Fragment to an Activity in XML. 3.For small displays: Add a Fragment to an Activity in code at Runtime. Replace Fragments with other Fragments as needed. 8

9 School of Engineering and Information and Communication Technology Fragments Example – Phone and Tablet UI Phone Tablet 9

10 School of Engineering and Information and Communication Technology Fragments Example – Source Files 10 AndroidManifest.xml build.grade (Module: app) MainActivity.java HeadlinesFragment.java ArticleFragment.java Ipsum.java \res\layout\news _articles.xml \res\layout- large\news_articl es.xml \res\layout\article _view.xml

11 School of Engineering and Information and Communication Technology Step 1: Create a Fragment class To create a fragment, we create a subclass of Fragment, and implement callback methods like onCreate(), onCreateView(), and onPause(). –Callback methods for Fragments are similar to Activities, but onCreateView() is different. –onCreateView() is used to define the layout. 11

12 School of Engineering and Information and Communication Technology public class ArticleFragment extends Fragment {... @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {... return inflater.inflate(R.layout.article_view, container, false); } Step 1: Create a Fragment class 12 ArticleFragment.java \res\layout\article _view.xml

13 School of Engineering and Information and Communication Technology Step 2: For Large Displays For large displays, we add a Fragment to an Activity in XML. 13 public class MainActivity extends FragmentActivity implements HeadlinesFragment.OnHeadlineSelectedListener { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.news_articles); MainActivity.java \res\layout- large\news_articl es.xml

14 School of Engineering and Information and Communication Technology Step 3: For Small Displays For small displays, we add and replace Fragments in code at Runtime. 14 public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.news_articles); if (findViewById(R.id.fragment_container) != null) { if (savedInstanceState != null) { return; } HeadlinesFragment firstFragment = new HeadlinesFragment(); firstFragment.setArguments(getIntent().getExtras()); getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, firstFragment).commit(); } } \res\layout\news _articles.xml public void onArticleSelected(int position) {... FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.fragment_container, newFragment); transaction.addToBackStack(null); transaction.commit(); } } MainActivity.java

15 School of Engineering and Information and Communication Technology Saving State An activity can be destroyed by either: –A system constraint, or E.g. when your activity is currently stopped and hasn’t been used for a long time, or when the current foreground activity requires more resources (in which case the system shuts down background processes to recover memory). –As part of the normal app behaviour. E.g. the user presses the ‘Back’ button, or the activity signals its own destruction by calling finish(). If the system destroys the activity due to a system constraint, when the user navigates back to it, the system will need to create a new instance using a set of saved data that describes the state of the activity when it was destroyed. 15

16 School of Engineering and Information and Communication Technology Saving State Your activity is also destroyed and recreated each time the user rotates the screen. –Information in Widgets (TextView, Buttons, etc.) will be persisted automatically by Android as long as you assign an ID to them. So that means most of the UI state is taken care of without issue. Only when you need to store other data does this become an issue. -This is done because your activity might need to load alternative resources to accommodate the change in rotation. 16

17 School of Engineering and Information and Communication Technology static final String BUTTON_PRESSES = "buttonPresses"; int counter = 0;... @Override public void onSaveInstanceState(Bundle savedInstanceState) { // Save the user's current game state savedInstanceState.putInt(BUTTON_PRESSES, counter); // Always call the superclass so it can save the view hierarchy state super.onSaveInstanceState(savedInstanceState); } Saving State To save your activity state: 17

18 School of Engineering and Information and Communication Technology Saving State To restore your activity state: 18 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Check whether we're recreating a previously destroyed instance if (savedInstanceState != null) { // Restore value of members from saved state counter = savedInstanceState.getInt(BUTTON_PRESSES); }... }

19 School of Engineering and Information and Communication Technology Material Design What is it? –“A new approach to cross-product, cross-platform design that uses tangible surfaces, bold graphic design, and meaningful motion to help define the way software should behave”. –An interface that incorporates “tactile surfaces, bold graphic design, and fluid motion to create beautiful, intuitive experiences”. It’s goal: –To develop a single underlying system that allows for a unified experience across platforms and device sizes. –It is the default visual language for Android 5.0+ devices. 19

20 School of Engineering and Information and Communication Technology Material Design - Demo 20

21 School of Engineering and Information and Communication Technology Material Design – Differences to iOS Android uses DP units, while iOS uses points. –1dp = 1px @1x (or 160dpi). On Android, apps can integrate deeply with other apps, removing the need to re-implement functionality (e.g. taking photos). Android provides a system Back button, in addition to Home and Overview for task switching. Android tabs should be placed at the top of the screen rather than the bottom with iOS. Long-presses should only be used to select an item, begin dragging it, or to do nothing. 21

22 School of Engineering and Information and Communication Technology Material Design – Density-independent pixels Density-independent pixels (DP): –1dp = 1px @1x (where 1x is 160dpi). Density buckets: 22 Density bucket Scaling factor Density MDPI1x160dpi HDPI1.5x240dpi XHDPI2x320dpi XXHDPI3x480dpi XXXHDPI4x640dpi

23 School of Engineering and Information and Communication Technology Material Design – Material in a 3D space The material environment is a 3D space Material has varying x and y dimensions (measured in dp), but a uniform thickness (1dp). 23


Download ppt "School of Engineering and Information and Communication Technology KIT305/KIT607 Mobile Application Development Android OS –Permissions (cont.), Fragments,"

Similar presentations


Ads by Google