SyntaxHighlighter JS

2013-02-06

Android Interview Questions: Level 1

These are basic entry-level Android interview questions suitable to ask over the phone.

  • What is an Activity?
    An activity is a single, focused thing a user can do. Usually an activity is associated with one UI window.

  • What are the lifecycle methods of an Activity?
    onCreate(Bundle): Activity initializer method.
    onStart(): Activity is about to become visible.
    onResume(): Activity visible in foreground. Called after creation and after pausing.
    onPause(): method invoked when user leaves the Activity. Usually partial UI visible.
    onStop(): Activity is no longer visible.
    onRestart()
    onDestroy(): called when Activity removed from memory

  • What do you usually do in the Activity onCreate method?
    1.) First call super.onCreate(Bundle) to execute superclass initializer
    2.) Initialize the UI by calling method setContentView(View) to define the UI layout and findViewById(int) to interact with UI widgets
    3.) Initialize any resources or saved state needed by the Activity

  • What do you usually do in the Activity onPause method?
    1.) First call super.onPause() to execute superclass method
    2.) Stop and release CPU and battery intensive resource used by Activity, e.g. camera, animation, GPS.
    3.) Save user changes. Note: do resource intensive saves like database or cloud writes in onStop() method.

  • When can an Activity be destroyed?
    When the back button is presses, when the screen rotates, when the finish()method is called, when the activity has been stopped for a long time and resource is reclaimed.

  • How do you save state data before an Activity is destroyed?
    Override the onSaveInstanceState(Bundle) method. To restore the activity state data, override onRestoreInstanceState(Bundle).

  • What is a Bundle?
    A Bundle can be used to save and restore instance state information of an Activity in the onSaveInstanceState and onRestoreInstanceState methods.

    private static final String STATE_MSG_KEY = "stateMsgKey";

    @Override
    public void onSaveInstanceState(Bundle saveState) {
        saveState.putString(STATE_MSG_KEY, "message value:);
        super.onSaveInstanceState(saveState);
    }

    @Override
    public void onRestoreInstanceState(Bundle saveState) {
        super.onRestoreInstanceState(saveState);
        String msgVal = saveState.getString(STATE_MSG_KEY);
    }


  • How can you tell if an Activity is new or has been restored?
    In the onCreate(Bundle) method, check to see if the Bundle parameter is null. If Bundle is null, then the Activity a new instance. If Bundle is not null, then the Activity has been restored.
  • How can you permanently save Activity data?
    Use SharedPreferences.

    private static final String PERM_MSG_KEY = "permMsgKey";
    private writeMessage() {
        SharedPreferences sp = getPreferences(Context.MODE_PRIVATE);
        SharedPreferences.Editor e = sp.edit();
        e.putString(PERM_MSG_KEY, "message value");
        e.commit();
    }

    private readMessage() {
        SharedPreferences sp = getPreferences(Context.MODE_PRIVATE);
        String msgVal = sp.getString(PERM_MSG_KEY, "default value");
    }

  • How do you define the "main" Activity that first gets called when you launch your app?
    In file AndroidManifest.xml, add the MAIN action and LAUNCHER category in the activity intent-filter.

    <activity android:name="com.jirawat.MainActivity" android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>


  • What is the AndroidManifest.xml file?
    This file contains all the essential information necessary to run an Android app. For example, it lists the minimum Android version required, the Java classes defining the activities, and user permissions necessary to run the app.

  • Where do you define the layout of the app UI?
    In XML files located at directory res/layout

  • Where do externalize text and string resources for the app?
    In the file res/values/strings.xml

  • What is R.java?
    During app compilation, the class R.java is automatically generated by aapt (Android Asset Packaging Tool). You can use the R.java class to access resources defined in the res directory from Java code.

    For example, to access the UI layout defined in the file res/layout/main_layout.xml in Java code, use R.layout.main_layout. To access text defined by the key app_name in res/values/strings.xml, use R.string.app_name.

  • How do you convert a string in string.xml into a Java string?
    String appName = getResources().getString(R.string.app_name);

  • What is an Intent?
    An Intent is an object that provides runtime bindings between separate components. An Intent can be used to start and communicate between two activities or apps.

  • How does an activity start another activity?
    Create an Intent then call the method startActivity with that Intent as a parameter.

    Intent intent = new Intent(this, ReceiveActivity.class);
    intent.putExtra("com.jirawat.MSG_KEY", "message value");
    startActivity(intent);


  • How do you get a result Intent back from another activity?1.) Use startActivityForResult instead of startActivity
    2.) Process result Intent in onActivityResult method

    private static final int REQUEST_CODE=314;

    private void sendActivityIntent() {
        Intent intent = new Intent(this, ReceiveActivity.class);
        intent.putExtra("com.jirawat.MSG_KEY", "message value");
        startActivityForResult(intent, REQUEST_CODE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent result) {
        if ( (requestCode == REQUEST_CODE) && (resultCode = RESULT_OK) ) {
            // Process result Intent
        }
    }


  • How do you specify the Android version the app supports?In file AndroidManifest.xml, add the uses-sdk xml element.
    <manifest>    <uses-sdk android:minSdkVersion="7" android:targetSdkVersion="11" /></manifest>

  • How do you check Android version in Java code?
    boolean isFroyoOrHigher =
      (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO);


  • What is a Fragment?
    A Fragment is a sub-activity. It is a UI or behavior that can be placed inside another parent Activity. It is used to create dynamic UI for different screen sizes.

  • How do you add fragments?
    Either via the fragment XML segment in the res/layout file or using the FragmentManager in the onCreate method.

    MyFragment f = new MyFragment();
    FragmentManager fm = getSupportFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();
    ft.add(R.id.fragment_container,f);
    ft.commit(); 

No comments:

Post a Comment