Saturday, January 3, 2015

Android Interview Questions - part 6

How to get image from gallery, in android?
Intent in = new Intent();
in.setType("image/*");
in.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(in, 0);

If I want to start some heavy weight functionalities that takes lot of battery power like starting animation or starting camera, should I do it in onCreate() or onStart() or onResume() of my activity? And where should I disable it?
Since heavy weight functions take too much of battery power, better do it just before your activity is ready to take user events. so do it in onResume().

When an activity is in stopped state, is it still in memory or not?
when onStop() is called, then activity is still in memory and all its states and variables are intact.

How to pass data between activities? let’s say pass user id, city, and password to next activity and display it.
Intent in = new Intent();
in.setAction("ACTION"); //this should match with other activity intent-filter
in.putExtra("uid","tech");
in.putExtra("city","Bangalore");
in.putExtra("pw","android");

How to take picture from camera in android?
//make a folder "pics" to store pics taken by the camera using this
final String directory = Environment.getExternalStoragePublicDirectory                                       (Environment.DIRECTORY_PICTURES) + "/pics/";
File nd = new File(directory);
nd.mkdirs();

//what is the picture name?
String file = directory+"myfile"+".jpg";
File myFile = new File(file);
try {
   myFile.createNewFile();
} catch (IOException e) {}       

Uri myUri = Uri.fromFile(myFile);

//prepare intent for starting camera
Intent in = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
in.putExtra(MediaStore.EXTRA_OUTPUT, myUri);

startActivityForResult(in, 1);

How to rotate an image in ImageView?
Matrix m=new Matrix();
imageView.setScaleType(ScaleType.MATRIX);  
m.postRotate((float) angle, pivX, pivY);
imageView.setImageMatrix(matrix);

final RotateAnimation rotateAnim = new RotateAnimation(0.0f, degree,
           RotateAnimation.RELATIVE_TO_SELF, 0.5f,
           RotateAnimation.RELATIVE_TO_SELF, 0.5f);


How to get current date in android?
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdate = new SimpleDateFormat("dd-MMM-yyyy");
String date = sdate.format(cal.getTime());

SimpleDateFormat dateFmt = new SimpleDateFormat("yyyyMMdd_HHmmss");
String date = dateFmt.format(new Date());


What is the difference between margin and padding in android?

Which of the following are appropriate for saving the state of an android applications?
Activity.onPause() is the only last guaranteed function to be called in activity life cycle, before killing an activity in all the scenarios.
So save all your persistent data in onPause().

What does the flag FLAG_ACTIVITY_NEW_TASK do here?
Intent in = new Intent();
in.setAction("com.android.myproject.MYACTION");
in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(in);

Description from developer android documentation:
If this flag is set, then this activity will become the start of a new task on this history stack.
When using this flag, if a task is already running for the activity you are now starting, then a new activity will not be started; instead, the current task will simply be brought to the front of the screen with the state it was last in.

What does the flag FLAG_ACTIVITY_SINGLE_INSTANCE do here?
Intent in = new Intent();
in.setAction("com.android.myproject.MYACTION");
in.setFlags(Intent.FLAG_ACTIVITY_SINGLE_INSTANCE);
startActivity(in);

This will start a new activity in a new task, where only this activity will be there in that task and no other components. if at all new components are launched from this new activity, they will be launched in a different task.

No comments:

Post a Comment