Ocak, 2018 tarihine ait yayınlar gösteriliyor

Launch Main Activity only one time on Android

Use  android:launchMode="singleTop"  to make sure that a new instance of  MainActivity  isn't spawned when you press the back button. Spawning a new instance of  MainActivity  is not memory efficient. Then, in the actual  SettingsActivity  you should override the home button to act like the back button: if (id == android.R.id.home) { onBackPressed(); } and you should display home as up, to  allow up navigation : this .getSupportActionBar().setDisplayHomeAsUpEnabled( true );

Differences between PreferenceChangeListener and SharedPreferenceChangeListener

PreferenceChangeListener  is not the same as a  SharedPreferenceChangeListener The differences are: SharedPreferenceChangeListener  is triggered after any value is saved to the SharedPreferences file. PreferenceChangeListener  is triggered before a value is saved to the SharedPreferences file. Because of this, it can prevent an invalid update to a preference.  PreferenceChangeListeners  are also attached to a single preference. Generally the flow goes like this: User updates a preference. PreferenceChangeListener  triggered for that preference. The new value is saved to the SharedPreference file. onSharedPreferenceChanged  listeners are triggered. Otherwise they act very similarly. In your activity you implement the  Preference.OnPreferenceChangeListener , override the  onPreferenceChange(Preference preference, Object newValue) . The  onPreferenceChange  method will return either true or false, depending on whether the preference should actually be saved. Step 1: I

Register SharedPreference

SharedPreferences sharedPreferences = PreferenceManager. getDefaultSharedPreferences (this); Register the listener sharedPreferences.registerOnSharedPreferenceChangeListener(this); Unregister the listener PreferenceManager. getDefaultSharedPreferences (this)          .unregisterOnSharedPreferenceChangeListener(this);

Creating menu bar on Android

onCreateOptionsMenu()   method should be override. The  showAsAction  attribute allows you to define how the action is displayed. For example, the  ifRoom  attribute defines that the action is only displayed in the action bar if there is sufficient screen space available. <? xml version= "1.0" encoding= "utf-8" ?> <menu xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: app = "http://schemas.android.com/apk/res-auto" > <item android :id= "@+id/action_settings" android :title= "@string/menu_title" app :showAsAction= "never" android :orderInCategory= "100" /> </menu> The   MenuInflator   class allows to inflate actions defined in an XML file and adds them to the action bar.   MenuInflator   can get accessed via the   getMenuInflator()   method from your   activity . The following example code demonstrates the creation of actions. @Overrid

Questions and Answers about Android

How do we tell Android that we'd like a menu item to show up as a button on the Toolbar? Add the showAsAction XML attribute to the menu item. < menu xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: app = "http://schemas.android.com/apk/res-auto" > < item android :title= "@string/search" android :orderInCategory= "1" app :showAsAction= "ifRoom" android :id= "@+id/action_search" /> </ menu > Which of the following activities can only be done after declaring a uses-permission in the manifest (rather than by using a system app)?  Hint: Take a moment to explore  http://developer.android.com/guide/topics/security/permissions.html Taking a photo Making a phone call Getting the user’s current location <-- The answer Accessing the details for a contact the user selects from the contact manager Which methods a

Question on Lifecycle Events on Android

What is the order that the lifecycle events are called  after  the device is rotated ? onPause, onStop, onDestroy, onCreate, onStart, onResume Rotating the device causes the Activity to be destroyed and recreated, so our lifecycle starts at onPause and ends at onResume. Note that we don't see onRestart, which only happens if the activity is stopped (but not destroyed) and then restarted. More Info: When we use   onSaveInstanceState, We're missing onStop and onDestroy, because those states happen after onSaveInstanceState is called . We at least see the lifecycle all the way through onSaveInstanceState and you'll see that the TextView gets filled with more logs as you rotate the device more.

ShareCompat.IntentBuilder in Android

IntentBuilder is a helper for constructing  ACTION_SEND  and  ACTION_SEND_MULTIPLE  sharing intents and starting activities to share content. The ComponentName and package name of the calling activity will be included. Source :  https://developer.android.com/reference/android/support/v4/app/ShareCompat.IntentBuilder.html A choose dialog appears from the bottom of the screen showing all the apps on the device that are able to handle this type of the intent. // TODO (1) Create a void method called shareText that accepts a String as a parameter public void shareText (String text) { // Do steps 2 - 4 within the shareText method // TODO (2) Create a String variable called mimeType and set it to "text/plain" String mimeType = "text/plain" ; // TODO (3) Create a title for the chooser window that will pop up String title = "First Share Text" ; // TODO (4) Use ShareCompat.IntentBuilder to build the Intent and start the choose ShareComp

Verify that Intent can be launched

// TODO (1) Create a method called openWebPage that accepts a String as a parameter // Do steps 2 - 4 within openWebPage     // TODO (2) Use Uri.parse to parse the String into a Uri     // TODO (3) Create an Intent with Intent.ACTION_VIEW and the webpage Uri as parameters      // TODO (4) Verify that this Intent can be launched and then call startActivity public void openWebPage(String url){      Uri uri = Uri. parse (url);      Intent intent = new Intent(Intent. ACTION_VIEW ,uri);      if(intent.resolveActivity(getPackageManager()) != null){          startActivity(intent);      } } When you call  startActivity()  or  startActivityForResult()  and pass it an implicit intent, the system  resolves the intent  to an app that can handle the intent and starts its corresponding  Activity . If there's more than one app that can handle the intent, the system presents the user with a dialog to pick which app to use. This page des